资源简介
本程序是mandelbrot集的在MPI下的并行实现,试验中采用了动态调度的并行程序设计方法。
代码片段和文件信息
#include “mpi.h“
#include
#include
#define MAX_DEPTH 256 //最大的迭代次数
#define WIDTH 40//1024
#define HIGHT 40//768
#define N (WIDTH)*(HIGHT)
//实际坐标的范围
float left_x = -2.0;
float left_y = -2.0;
float right_x = 2.0;
float right_y = 2.0;
struct complex1{
float real;
float img;
};
int cal(struct complex1 c);
void write_count(int *countschar *strint wint h);
int main(int argcchar *argv[])
{
int myrankmsgtag1=1msgtag2=2nproclocal_Nnrecv;
int ijjy;
int *counts *local_count;
int *isIdleipiproc;
float dxdyxy;
MPI_Status status;
struct complex1 c;
MPI_Init(&argc&argv);
MPI_Comm_size(MPI_COMM_WORLD &nproc);
MPI_Comm_rank(MPI_COMM_WORLD &myrank);
isIdle=(int *)malloc(nproc*sizeof(int));//空闲标志
MPI_Bcast(&left_x 1 MPI_FLOAT 0 MPI_COMM_WORLD);
MPI_Bcast(&left_y 1 MPI_FLOAT 0 MPI_COMM_WORLD);
MPI_Bcast(&right_x 1 MPI_FLOAT 0 MPI_COMM_WORLD);
MPI_Bcast(&right_y 1 MPI_FLOAT 0 MPI_COMM_WORLD);
dx = (right_x - left_x) / WIDTH;
dy = (right_y - left_y) / HIGHT;
if(myrank==0)
{
for (i=0;i {
isIdle[i]=1;
}
j=0;
counts = (int *)malloc(N * sizeof(int));
local_count = (int *)malloc(WIDTH * sizeof(int));
for (ip=1;ip {
if(isIdle[ip])
{
isIdle[ip]=0;
printf(“rank:%d send j:%d -> rank:%d....\n“myrankjip);
MPI_Send(&j1MPI_INTipjMPI_COMM_WORLD);
printf(“rank:%d send j:%d -> rank:%d....done\n“myrankjip);
j++;
}
}
nrecv=0;
while(j {
nrecv=nrecv+1;
MPI_Recv(&jy1MPI_INTMPI_ANY_SOURCEMPI_ANY_TAGMPI_COMM_WORLD&status);
iproc=status.MPI_SOURCE;
printf(“rank:%d receive jy:%d <- rank:%d\n“myrankjyiproc);
MPI_Recv(local_countWIDTHMPI_FLOATiprocjyMPI_COMM_WORLD&status);
printf(“rank:%d receive local_count <- rank:%d nrecv:%d\n“myrankiprocnrecv);
for (i=0;i {
counts[i+jy*WIDTH]=local_count[i];
}
isIdle[iproc]=1;
ip=iproc;
MPI_Send(&j1MPI_INTipjMPI_COMM_WORLD);//分配任务
printf(“rank:%d send j:%d -> rank:%d\n“myrankjip);
isIdle[ip]=0;
j++;
}
printf(“rank:%d now j:%d nrecv:%d this\n“myranknrecvj);
for (;nrecv {
MPI_Recv(&jy1MPI_INTMPI_ANY_SOURCEMPI_ANY_TAGMPI_COMM_WORLD&status);
iproc=status.MPI_SOURCE;
printf(“rank:%d receive jy:%d <- rank:%d\n“myrankjyiproc);
MPI_Recv(local_countWIDTHMPI_FLOATiprocjyMPI_COMM_WORLD&status);
printf(“rank:%d receive local_count <- rank:%d\n“myrankiproc);
- 上一篇:NRFL2401无线模块
- 下一篇:mandelbrot的静态调度实现
评论
共有 条评论