Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<mpi.h>
- #include<stdio.h>
- #include<stdlib.h>
- /**
- * @author cristian.chilipirea
- *
- */
- int main(int argc, char * argv[]) {
- int rank;
- int nProcesses;
- MPI_Init(&argc, &argv);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- MPI_Comm_size(MPI_COMM_WORLD, &nProcesses);
- printf("Hello from %i/%i\n", rank, nProcesses);
- int dest, src;
- MPI_Status Stat;
- //if(rank==0) { // This code is run by a single process
- int polynomialSize;
- int n;
- int i;
- int j;
- float x;
- FILE * polFunctionFile = fopen("a1.txt","rt");
- fscanf(polFunctionFile, "%i", &polynomialSize);
- float * a = malloc(sizeof(float)*polynomialSize);
- for(i=0;i<polynomialSize;i++) {
- fscanf(polFunctionFile, "%f", a+i);
- }
- fclose(polFunctionFile);
- for(x=0; x<3; x+=1) {
- float sum = 0;
- float xPowI = 1;
- float out_pow, in_pow;
- float out_sum = 0;
- float in_sum;
- /*for(i=0;i<polynomialSize;i++) {
- sum += xPowI * a[i];
- xPowI *= x; // more efficient than pow(x,i);
- }
- printf("%f ", sum);*/
- //printf("%f\n", x);
- //for(i=0;i<polynomialSize;i++) {
- if(rank==0){
- dest = rank+1;
- out_sum += xPowI * a[rank];
- out_pow = xPowI;
- out_pow *= x;
- MPI_Send(&out_pow, 1, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);
- MPI_Send(&out_sum, 1, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);
- }
- else if(rank == nProcesses-1){
- src = rank-1;
- //dest = rank+1;
- MPI_Recv(&in_pow, 1, MPI_FLOAT, src, 1, MPI_COMM_WORLD,&Stat);
- MPI_Recv(&in_sum, 1, MPI_FLOAT, src, 1, MPI_COMM_WORLD,&Stat);
- //printf("%f\n", in_pow);
- out_sum = in_sum + in_pow * a[rank];
- //out_pow = in_pow * x;
- printf("%f \n", out_sum);
- }
- else{
- src = rank - 1;
- dest = rank+1;
- MPI_Recv(&in_pow, 1, MPI_FLOAT, src, 1, MPI_COMM_WORLD,&Stat);
- sum += in_pow * a[rank];
- out_pow = in_pow* x;
- MPI_Send(&out_pow, 1, MPI_FLOAT, dest, 1, MPI_COMM_WORLD);
- }
- //}
- }
- //}
- /*for(x = 0;x<3;x++){
- float sum = 0;
- float xPowI = 1;
- }*/
- MPI_Finalize();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement