我正在尝试在C中编写一个函数,每个处理器都会打印自己的数据.这是我有的:
void print_mesh(int p,int myid,int** U0,int X,int Y){
int i,m,n;
for(i=0;i<p;i++){
if(myid==i){
printf("myid=%d\n",myid);
for(n=0;n<X;n++){
for(m=0;m<Y;m++){
printf("%d ",U0[n][m]);
}
printf("\n");
}
}
else MPI_Barrier(MPI_COMM_WORLD);
}
}
Run Code Online (Sandbox Code Playgroud)
它由于某种原因不起作用.阵列全部混合打印.你有什么见解,为什么这不起作用?还有其他有用的想法吗?如果可能的话,我不想在主进程中发送整个数组.另外,我不想使用预编译功能.
我需要从一个进程发送一个矩阵列,然后从另一个进程接收它.我尝试运行以下程序,但是我得到了一个奇怪的结果(至少我是这么认为的); 仅复制矩阵的第一个元素,并且某些矩阵元素意外更改.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "mpi.h"
void swap(int* a,int* b){
int temp;
temp=*a;
*a=*b;
*b=temp;
}
void print_matrix(double** A,int n){
int i,j;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf("%f ",A[i][j]);
}
printf("\n");
}
}
int main(int argc, char *argv[]){
int i,j,k,l,n,myid,p,maxp;
double **A;
MPI_Datatype col_type;
MPI_Status status;
n=3;
A=malloc(n*sizeof(double*)); /*allocating memory */
for(i=0;i<n;i++)
A[i]=malloc(n*sizeof(double));
A[0][0]=-1;
A[0][1]=2;
A[0][2]=-1;
A[1][0]=2;
A[1][1]=-1;
A[1][2]=0;
A[2][0]=1;
A[2][1]=7;
A[2][2]=-3;
MPI_Init(&argc,&argv);
MPI_Type_vector(n, 1, n, MPI_DOUBLE,&col_type);
MPI_Type_commit(&col_type);
MPI_Comm_size(MPI_COMM_WORLD,&p);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
if(myid==0){
printf("Starting Method with p=%d\n",p);
print_matrix(A,n);
} …Run Code Online (Sandbox Code Playgroud)