多线程消息传递(MPI)库以并行化
目的:
计算输入向量的前缀和
注意:向量的顺序应被comm_sz整除
在代码中编码C:block或VB 2012
我想更正此代码的错误
并纠正错误
为什么会出现此错误?错误:字符串常量|前的预期声明说明符或'...'在C代码中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <mpi.h>
void Check_for_error(int local_ok, char fname[], char message[]);
void Get_n(int argc, char* argv[], int* n_p, int* local_n_p);
void Read_vector(char prompt[], double loc_vect[], int n, int loc_n);
void Print_vector(char title[], double loc_vect[], int n, int loc_n);
void Compute_prefix_sums(double loc_vect[], double loc_prefix_sums[];
int n, int loc_n);
int my_rank, comm_sz;
MPI_Comm comm;
int main(int argc, char*argv[]){
double *loc_vect, *loc_prefix_sums;
int n ;
int loc_n;
int MPI_Init(int *argc, char ***argv);
comm = MPI_COMM_WORLD;
int MPI_Comm_rank( MPI_Comm comm, int my_rank )
int MPI_Comm_size( MPI_Comm comm, int *comm_sz );
Get_n(argc , argv , &n , &loc_n);
loc_vect = (double*)malloc(loc_n*sizeof(double));
loc_prefix_sums = (double*)malloc(loc_n*sizeof(double));
Read_vector("Enter the vector", loc_vect, n, loc_n);
Print_vector("Input vector", loc_vect, n, loc_n);
Compute_prefix_sums(loc_vect, loc_prefix_sums, n, loc_n);
Print_vector("Prefix sums", loc_prefix_sums, n, loc_n);
free(loc_vect);
free(loc_prefix_sums);
MPI_Finalize();
return 0;
}
void Check_for_error(int local_ok, char fname[], char message[])
//void Check_for_error(int local_ok , char fname[] , char message[] )
{
int ok ;
MPI_Allreduce(&local_ok, &ok, 1, MPI_INT, MPI_MIN, comm);
if (ok == 0) {
int my_rank ;
MPI_Comm_rank(comm, &my_rank)
if (my_rank == 0) {
fprintf(stderr, "Proc %d > In %s, %s\n", my_rank, fname, message);
fflush(stderr);
}
MPI_Finalize();
exit(-1);
}
}
void Get_n(int argc, char* argv[], int* n_p, int* local_n_p)
{
int local_ok = 1;
if (my_rank == 0){
if (argc != 2)
*n_p = 0;
else
*n_p = strtol(argv[1], NULL, 10);
}
MPI_Bcast(n_p, 1, MPI_INT, 0, comm);
if (*n_p <= 0 || *n_p % comm_sz != 0){
local_ok = 0;
Check_for_error(local_ok,"Get_n","n should be > 0 and evenly divisible by comm_sz");
*local_n_p = *n_p / comm_sz;
} /* Get_n */
}
void Read_vector(char prompt[], double loc_vect[], int n, int loc_n) {
int i;
double *tmp = NULL;
if (my_rank == 0) {
tmp = (double*)malloc(n*sizeof(double));
printf("%s\n", prompt);
for (i = 0;i< n; i++)
scanf("%1f\",tmp[i]);
MPI_Scatter(tmp, loc_n, MPI_DOUBLE, loc_vect, loc_n, MPI_DOUBLE, 0,
comm);
free(tmp);
} else {
MPI_Scatter(tmp, loc_n, MPI_DOUBLE, loc_vect, loc_n, MPI_DOUBLE, 0,
comm);
}
} /* Read_vector
void Print_vector(char title[], double loc_vect[], int n, int loc_n) {
int i;
double * tmp = NULL;
if (my_rank == 0) {
tmp = (double*)malloc(n*sizeof(double));
MPI_Gather(loc_vect, loc_n, MPI_DOUBLE, tmp, loc_n, MPI_DOUBLE, 0,
comm);
printf("%s\n ", title);
for (i = 0; i < n; i++)
printf("%.2f ", tmp[i]);
printf("\n");
free(tmp);
} else {
MPI_Gather(loc_vect, loc_n, MPI_DOUBLE, tmp, loc_n, MPI_DOUBLE, 0,
comm);
}
}
/*-------------------------------------------------------------------
* Function: Compute_prefix_sums
* Purpose: Compute the prefix sums for the components assigned to
* this process
* In args: loc_vect, n, loc_n
* Out args: loc_prefix_sums
*/
void Compute_prefix_sums(double loc_vect[], double loc_prefix_sums[], int n,
int loc_n) {
int loc_i, src, dest;
double sum_of_preceding;
/* First compute prefix sums of my local vector */
loc_prefix_sums[0] = loc_vect[0];
for (loc_i = 1; loc_i < loc_n; loc_i++)
loc_prefix_sums[loc_i] = loc_prefix_sums[loc_i-1] + loc_vect[loc_i];
# ifdef DEBUG
Print_vector("After local prefix sums calc", loc_prefix_sums, n, loc_n);
# endif
/* Now figure which process precedes me and which process succeeds me */
src = my_rank - 1;
dest = my_rank + 1;
if (my_rank != 0) {
/* If I'm not 0 receive sum of preceding components */
MPI_Recv(&sum_of_preceding, 1, MPI_DOUBLE, src, 0, comm,
MPI_STATUS_IGNORE);
# ifdef DEBUG
printf("Proc %d > /**< /**< /**< */ */ */received %f from %d\n", my_rank,
sum_of_preceding, src);
# endif
/* Add in sum of preceding components to my prefix sums */
for (loc_i = 0; loc_i < n; loc_i++)
loc_prefix_sums[loc_i] += sum_of_preceding;
}