Anyone want to help me with some C coding? I'm a newbie.

Recommended Answers

All 8 Replies

Anyone want to help me with C Code?

try posting in the C++/C forum, you will get much more help there.

hi, I'm a CS student and as far as my experience tells me, you should follow E.Balaguruswamy ANSI C book and of course,get hold of A good C compiler(MS Visual C++/Turbo C++) for running the e.g. programs given in the book to get a feel of the use of the language features.Actually,what matters is your understanding of the logic behind a problem,a program is just a mode of translation of your logical soln. to it.And of course,practice a bit evry day will gonna help too.SO HAPPY PROGRAMMING

>I'm a CS student and as far as my experience tells me
I'm thinking you need some more experience if you can't tell that 2004 was two years ago.

>I'm a CS student and as far as my experience tells me
I'm thinking you need some more experience if you can't tell that 2004 was two years ago.

But 2004 was only 1.5 years ago :-)

>But 2004 was only 1.5 years ago :-)
June 26, 2004 was the last post. It's now June 23, 2006. That's close enough to two years to get a slap from me for being a pedant. ;)

I need help to make the following code work (mpi with C):

#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define N 6


int main(int argc, char **argv )
{
/* variable definitions */
int rank, size, i, j, start1, start2, n,k,l,m,z,w,p,q, des1,des2,e;
float sum;
float a[100][100], b[100], c[100][100], d[100], x[100], y[100], result[100];
FILE *fp1;


/* Initialise MPI */
MPI_Status status;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );



//μεταβλητές για να κρατω την αρχή κάθε τμήματος N/size
start1=0;
start2=0;



//αν είσαι ο master
if (rank== 0) {


/*printf("Enter the number of the equations N:");*/
/*scanf("%d/n",&N);*/
/*διαβάζει τα p τμήματα του πίνακα Α*/
fp1 = fopen("A.txt","r");       //ανοιξε το αρχείο Α


for (des1=1; des1<=size; des1++)                        //για τον κάθε εργόζομενο που διαθέτεις
for (i = start1; i < (start1+N/size); i++)
for (j=0; j<N; j++){
fscanf(fp1,"%f", &a[j]);
//στείλτου ένα ένα τα στοιχεία των αντίστοιχων N/size γραμμών
MPI_Send(a+j,1, MPI_FLOAT,des1,100,MPI_COMM_WORLD);
}


close(fp1);


/*διαβαζει τα p τμήματα του πινακα β*/
fp1 = fopen("b.txt","r");


for (des2=1; des2<=size; des2++)
//όμοια στέλνει στους εργαζόμενους τα αντίστοιχα στοιχεία του αρχείου b
for (i = start2; i < (start2+N/size); i++){
fscanf(fp1,"%f", &b);


/*στέλνει τα p τμήματα των γραμμών των πινάκων Α & b στους εργαζόμενους*/
MPI_Send(&b, 1, MPI_FLOAT, des2, 100, MPI_COMM_WORLD);
}


close(fp1);
}


if (rank!=0){                           //οι εργαζόμενοι
n=N/size;



if (rank==1){                   //αν είσαι ο πρώτος εργαζόμενος
for (i=0; i<n; i++)
for (j=0; j<N; j++)
MPI_Recv(c+j, 1, MPI_FLOAT, 0, 100, MPI_COMM_WORLD, &status);
//πάρε τα πρώτα στοιχεία του αρχείου Α των N/size πρώτων γραμμών


for (k=0; k<n; k++)
MPI_Recv(&d[k], 1, MPI_FLOAT, 0, 100, MPI_COMM_WORLD, &status);
//πάρε τα πρώτα στοιχεία του αρχείου b των N/size πρώτων γραμμών


/*υπολογισμός x*/


x[0] = d[0] / c[0][0];                                          //υπολογίζει το χ[0]
MPI_Send(&x[0],1,MPI_FLOAT,rank+1,100,MPI_COMM_WORLD);
//και το στέλνει σε όλους τους άλλους εργαζόμενους


for(l = 1; l < n; l++) {
sum = 0.0;
for(m = 0; m < l; m++)
sum = sum + a[l][m] * x[m];


x[l] = (d[l] - sum) / c[l][l];
// μόλις υπολογίσει ένα από τα x αμέσως το στέλνει στους άλλους εργαζόμενους


MPI_Send(&x[l],1, MPI_FLOAT, rank+1,100,MPI_COMM_WORLD);
}
}



if ((rank>1) && (rank<size)) {          //αν είσαι κάποιος από τους ενδιάμεσους εργαζόμενους


k=i*N/size-1;


for (i=k-n; i<k; i++)
for (j=0; j<N; j++)
MPI_Recv(c+j,1, MPI_FLOAT, 0, 100, MPI_COMM_WORLD, &status);
//πάρε τα στοιχεία του αρχείου Α των N/size γραμμών που του αντιστοιχούν


for (z=k-n; z<k; z++)
MPI_Recv(&d[z], 1, MPI_FLOAT, 0, 100, MPI_COMM_WORLD, &status);
//πάρε τα στοιχεία του αρχείου b των N/size γραμμών που του αντιστοιχούν


/*λαμβάνει τα υπολογισμένα */


MPI_Recv(&y[0], 1, MPI_FLOAT, 1, 100,  MPI_COMM_WORLD, &status);
//λαμβάνει το υπολογισμένο x[0]


for (w=1; w<n; w++)
MPI_Recv(&y[w], 1, MPI_FLOAT,1,100,  MPI_COMM_WORLD, &status);
//λαμβάνει τα υπόλοιπα x που υπολόγισε ο εργαζόμενος 1


if (rank!=2)
for (w=n; w<k-n; w++)
MPI_Recv(&y[w],1, MPI_FLOAT,rank-1, 100, MPI_COMM_WORLD, &status);
//πάρε τα x που υπολόγισε ο εργαζόμενος rank-1


/*υπολογισμός x*/
for(l = k-n; l < k; l++) {
sum = 0.0;
for(m = 0; m < l; m++)
sum = sum + a[l][m] * x[m];


x[l] = (d[l] - sum) / c[l][l];                              //υπολόγιζει τα x που του αντιστοιχούν


y[l] = x[l];


}


if (rank==(size-1))
for (p=0; p<k; k++)
MPI_Send(&y[p],1,MPI_FLOAT, rank+1,100,MPI_COMM_WORLD);
else{
for (q=n; q<k; q++)
MPI_Send(&x[q],1,MPI_FLOAT,rank+1,100,MPI_COMM_WORLD);
}
}



if (rank==size){                                                               //αν είσαι ο τελευταίος εργαζόμενος
k=rank*N/size-1;


for (i=k-n; i<k; i++)
for (j=0; j<N; j++)
MPI_Recv(c+j,1,MPI_FLOAT,0,100,MPI_COMM_WORLD, &status);
//πάρε τα στοιχεία του αρχείου Α των N/size γραμμών που του αντιστοιχούν


for (z=k-n; z<k; z++)
MPI_Recv(&d[z],1,MPI_FLOAT,0,100,MPI_COMM_WORLD,&status);
//πάρε τα στοιχεία του αρχείου b των N/size γραμμών που του αντιστοιχούν


for (w=0; w<k-n; w++)
MPI_Recv(&result[w],1, MPI_FLOAT, rank-1, 100, MPI_COMM_WORLD, &status);
//λαμβάνει τα  x που υπολόγισαν οι προηγούμενοι εργαζόμενοι


/*υπολογισμός x*/
for(l = k-n; l < k; l++) {
sum = 0.0;
for(m = 0; m < l; m++)
sum = sum + a[l][m] * x[m];


x[l] = (d[l] - sum) / c[l][l];                            //υπολόγιζει τα x που του αντιστοιχούν


result[l] = x[l];
}


printf("Το διάνυσμα x είναι: ");
for (i=0; i<N; i++)
printf("%f%d\n x",result);                             //εκτυπώνει όλα x
}
}



/* Finalise MPI */
MPI_Finalize();
return 0;
}
commented: Go away until you learn how to use a forum! -4

> >But 2004 was only 1.5 years ago :-)
> June 26, 2004 was the last post. It's now June 23, 2006.
Well silia55 really raised the bar on this one, by being well over 2 YEARS late with the useless bump, showing NO regard WHATSOEVER for the forum rules.

Move along, nothing to see here until lets say the 2012 Olympics roll around. That seems like a nice topical point to suggest at the moment.

It also gives some utter dimwit something to search for at the appropriate time, and dribble (like some Pavlovian dog) all over the dead post.

commented: Way too funny... my ribs!... the pain!!!.. XD +3
commented: Your feats of self-satisfaction are psychotic. +0
commented: equalizer +8
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.