example of MPI

#include<stdio.h>
#include<mpi.h>

int main(int argc, char **argv)
{
	int myid, numprocs;
	int source = 0;
	int *sbuf;
	int rbuf[2]; 
	int i;
	
	MPI_Init(&argc, &argv);
	
	MPI_Comm_rank(MPI_COMM_WORLD, &myid);
	MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
    
    if(myid == source) {
    	sbuf=(int *)malloc(numprocs*5*sizeof(int));
    	
    	for(int i=0;i<numprocs*5;i++) {
    		sbuf[i]=i;
		}
	}

    // your code here
	MPI_Scatter(sbuf+5,2,MPI_INT,rbuf,2,MPI_INT,0,MPI_COMM_WORLD);
	// end of your code
	
	printf("Now is process %d: ", myid);
	for(i=0;i<2;i++) {
		printf("array[%d]=%d\t", i, rbuf[i]);
	}
	printf("\n");
	
	
	
	MPI_Finalize();
	return 0;
} 
posted @ 2023-02-13 11:05  MITE's_BKY  阅读(18)  评论(0)    收藏  举报