c - 尝试运行 MPI 矩阵乘法示例

标签 c unix mpi

我正在尝试运行我在网上找到的代码,以帮助更好地理解 MPI,但当我尝试编译以下代码时,出现错误。

我收到以下信息:

$ mpicc -o mpimm mpimm.c
mpimm.c: In function ‘main’:
mpimm.c:10: error: storage size of ‘start’ isn’t known
mpimm.c:10: error: storage size of ‘stop’ isn’t known

下面是示例代码

#include "stdio.h"
#include "mpi.h"
#define N  		500        /* number of rows and columns in matrix */

MPI_Status status;

double a[N][N],b[N][N],c[N][N];	 //matrix used 
       
main(int argc, char **argv){
	struct timeval start, stop;
	int numberOfTasks,
	mtype,
	taskID,
	numberOfWorkers,
	source,
	destination,
	rows,
	averageRow,
	extra,
	offset,i,j,k;
	//first initialization
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &taskID);
	MPI_Comm_size(MPI_COMM_WORLD, &numberOfTasks);

	numberOfWorkers = numberOfTasks-1;

 //---------------------------- master ----------------------------//
	if (taskID == 0) {
		for (i=0; i<N; i++) {
			for (j=0; j<N; j++) {   
				a[i][j]= 1.0;
				b[i][j]= 2.0;
			}
		}

    /* send matrix data to the worker tasks */
	
	gettimeofday(&start, 0);
	
    averageRow = N/numberOfWorkers; //average rows per worker
	extra= N%numberOfWorkers;		//extra rows
    offset = 0;
    
		for (destination=1; destination<=numberOfWorkers; destination++){			   	
	
			if(destination<=extra){
				rows = averageRow+1;
			}else{
				rows = averageRow;
			}
			mtype = 1;
			MPI_Send(&offset, 1, MPI_INT, destination, mtype, MPI_COMM_WORLD);
			MPI_Send(&rows, 1, MPI_INT, destination, mtype, MPI_COMM_WORLD);
			MPI_Send(&a[offset][0], rows*N, MPI_DOUBLE,destination,mtype, MPI_COMM_WORLD);
			MPI_Send(&b, N*N, MPI_DOUBLE, destination, mtype, MPI_COMM_WORLD);
			offset = offset + rows;
		}

    /* wait for results from all worker tasks */
		for (i=1; i<=numberOfWorkers; i++){
			mtype = 2;
			source = i;
			MPI_Recv(&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
			MPI_Recv(&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
			MPI_Recv(&c[offset][0], rows*N, MPI_DOUBLE, source, mtype, MPI_COMM_WORLD, &status);
		}
		gettimeofday(&stop, 0);
	
		
		printf("Upper Left = %6.2f    Upper Right = %6.2f \n",c[0][0],c[0][N-1]);
		printf("Lower Left = %6.2f    Lower Right = %6.2f \n",c[N-1][0],c[N-1][N-1]);
		
		fprintf(stdout,"Time = %.6f\n\n",(stop.tv_sec+stop.tv_usec*1e-6)-(start.tv_sec+start.tv_usec*1e-6));

 
	} 

  /*---------------------------- worker----------------------------*/
	if (taskID > 0) {
		source = 0;
		mtype = 1;
		MPI_Recv(&offset, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
		MPI_Recv(&rows, 1, MPI_INT, source, mtype, MPI_COMM_WORLD, &status);
		MPI_Recv(&a, rows*N, MPI_DOUBLE, source, mtype, MPI_COMM_WORLD, &status);
		MPI_Recv(&b, N*N, MPI_DOUBLE, source, mtype, MPI_COMM_WORLD, &status);
  
    /* Matrix multiplication */
		for (k=0; k<N; k++)
			for (i=0; i<rows; i++) {
				c[i][k] = 0.0;
				for (j=0; j<N; j++)
				c[i][k] = c[i][k] + a[i][j] * b[j][k];
			}

			mtype = 2;
		MPI_Send(&offset, 1, MPI_INT, 0, mtype, MPI_COMM_WORLD);
		MPI_Send(&rows, 1, MPI_INT, 0, mtype, MPI_COMM_WORLD);
		MPI_Send(&c, rows*N, MPI_DOUBLE, 0, mtype, MPI_COMM_WORLD);
	}  
    
  MPI_Finalize();
} 

最佳答案

struct timeval定义于 <time.h> ,您尚未将其包括在内。

gettimeofday很好,但更“mpi”的方法是使用 MPI_Wtime() 。该例程返回一个 double 值,您可以避免 struct timeval完全是问题。

关于c - 尝试运行 MPI 矩阵乘法示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40568375/

相关文章:

android - 在 Android 中处理 unix 信号

c - 原始数据的指针运算

c - 优化双循环

c - 线程安全随机数

c - 为什么这个短程序会产生这样的输出?

port - 将 MPI 设置为在一系列端口上运行

python - MPI 意外执行

c - 如何收集大型 Makefile 项目的所有依赖项?

bash - 如何从 csv 列中获取第一个和最后一个元素

c - 为 Unixlike 文件系统构建 inode 方法期间出现 malloc 错误