人気ブログランキング | 話題のタグを見る
(moved to http://tmasada.cocolog-nifty.com/blog/)
グローバル配列 ・・・ MPIプログラミング初経験とぃぅ
MPICH2のサンプル・プログラムであるcpi.cをちょっと直して,グローバル配列のテストをしました.テストのために,あえて時間のかかることをやってます.

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

double f(double);

double f(double a)
{
return (4.0 / (1.0 + a*a));
}

int main(int argc,char *argv[])
{
int n, myid, numprocs, i;
double PI25DT = 3.141592653589793238462643;
double mypi, pi, h, sum, x;
double startwtime = 0.0, endwtime;
int namelen;
char processor_name[MPI_MAX_PROCESSOR_NAME];

int globaln, rank, offset;
double *localarray;
double value;
MPI_Win win;

MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
MPI_Get_processor_name(processor_name,&namelen);

fprintf(stdout,"Process %d of %d is on %s\n",
myid, numprocs, processor_name);
fflush(stdout);

n = 100000; /* default # of rectangles */
globaln = n * numprocs;
MPI_Alloc_mem(n * sizeof(double), MPI_INFO_NULL, &localarray);
MPI_Win_create(localarray, n * sizeof(double), sizeof(double),
MPI_INFO_NULL, MPI_COMM_WORLD, &win);

if (myid == 0) startwtime = MPI_Wtime();

h = 1.0 / (double) globaln;
for (i = myid; i < globaln; i += numprocs) //あえて時間をくうことやってるとぃぅ
{
x = h * ((double)i + 0.5);
value = f(x);
rank = i / n;
offset = i % n;
MPI_Win_lock(MPI_LOCK_EXCLUSIVE, rank, 0, win);
if (rank == myid) {
localarray[offset] = value;
} else {
MPI_Put(&value, 1, MPI_DOUBLE,
rank, offset, 1, MPI_DOUBLE, win);
}
MPI_Win_unlock(rank, win);
}

MPI_Barrier(MPI_COMM_WORLD); //問:これ無かったらどうなるでしょうか

sum = 0.0;
for (i = 0; i < n; i ++) sum += localarray[i];

mypi = h * sum;
MPI_Reduce(&mypi, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);

if (myid == 0) {
endwtime = MPI_Wtime();
printf("pi is approximately %.16f, Error is %.16f\n",
pi, fabs(pi - PI25DT));
printf("wall clock time = %f\n", endwtime-startwtime);
fflush(stdout);
}

MPI_Win_free(&win);
MPI_Finalize();
return 0;
}

by tmasada2 | 2008-09-14 14:45 | らららルルル
<< ブックオフ ・・・ CPOPの... 韓国でのタン・ウェイウェイ ・... >>