MPI.COMM_WORLD.Bcast(d, 1, 0,MPI.DOUBLE, 0);
MPI.COMM_WORLD.Send(a,0,a.length,MPI.DOUBLE,dest,0);
MPI.COMM_WORLD.Send(b,0,b.length,MPI.DOUBLE,dest,0);
MPI.COMM_WORLD.Recv(a,0,d[0],MPI.DOUBLE,0,0);
MPI.COMM_WORLD.Recv(b,0,d[0],MPI.DOUBLE,0,0);
for (int i=0; i<d[0];i++){
sum[0]+=a[i]*b[i];
}
MPI.COMM_WORLD.Reduce(sum,0,result,0,1,MPI.DOUBLE,MPI.SUM,0);
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <signal.h>
#define MYTAG 1
int myid, j;
char processor_name[MPI_MAX_PROCESSOR_NAME];
double startwtime = 0.0, endwtime;
int main(int argc,char *argv[])
{
int total, n, numprocs, i, dest;
double *a, *b, sum, result;
int namelen;
MPI_Status status;
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);
if (myid == 0) {
total = atoi(argv[1]);
}
printf("Process %d of %d is on %s\n",
myid, numprocs, processor_name);
startwtime = MPI_Wtime();
n = total / numprocs + 1;
MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
a = malloc(n*sizeof(double));
b = malloc(n*sizeof(double));
if ((a == NULL) || (b == NULL)) {
fprintf(stderr,"Error allocating vectors (not enough memory?)\n");
exit(1);
}
if (myid == 0) {
for (dest=1; dest < numprocs; dest++) {
for (i=0; i < n; i++) {
a[i] = 4294967296;//rand();
b[i] = 4294967296;//rand();
}
MPI_Send(a, n, MPI_INT, dest, MYTAG, MPI_COMM_WORLD);
MPI_Send(b, n, MPI_INT, dest, MYTAG, MPI_COMM_WORLD);
}
n = total - n*(numprocs-1);
for (i=0; i < n; i++) {
a[i] = rand();
b[i] = rand();
}
} else {
MPI_Recv(a, n, MPI_INT, 0, MYTAG, MPI_COMM_WORLD, &status);
MPI_Recv(b, n, MPI_INT, 0, MYTAG, MPI_COMM_WORLD, &status);
}
printf("Process %d on node %s starting calc at %f sec\n",
myid, processor_name, MPI_Wtime()-startwtime);
sum = 0.0;
for (i=0; i<n; i++)
sum += a[i]*b[i];
printf("Process %d on node %s ending calc at %f sec\n",
myid, processor_name, MPI_Wtime()-startwtime);
MPI_Reduce(&sum, &result, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
if (myid == 0) {
endwtime = MPI_Wtime();
printf("Answer is %f\n", result);
printf("wall clock time = %f\n", endwtime-startwtime);
fflush(stdout);
}
MPI_Finalize();
return 0;
}
import mpi.*;
import java.util.*;
public class scalar {
public static void main(String args[]){
MPI.Init(args);
double[] result = new double[1];
int me = MPI.COMM_WORLD.Rank();
int size = MPI.COMM_WORLD.Size();
double startwtime=0.0;
double endwtime=0.0;
int total = 99999999;
int[] d = new int[1];
d[0] = total/size+1;
double[] a = new double[d[0]];
double[] b = new double[d[0]];
Random r = new Random();
MPI.COMM_WORLD.Bcast(d, 1, 0,MPI.INT, 0);
if (me == 0){
startwtime = MPI.Wtime();
for (int dest=1; dest<size;dest++){
for (int i=0; i<d[0]; i++){
a[i] = r.nextDouble();
b[i] = r.nextDouble();
}
MPI.COMM_WORLD.Send(a,0,a.length,MPI.INT,dest,0);
MPI.COMM_WORLD.Send(b,0,b.length,MPI.INT,dest,0);
}
d[0] = total - d[0]*(size-1);
for (int i=0; i<d[0];i++){
a[i] = r.nextDouble();
b[i] = r.nextDouble();
}
} else {
MPI.COMM_WORLD.Recv(a,0,d[0],MPI.INT,0,0);
MPI.COMM_WORLD.Recv(b,0,d[0],MPI.INT,0,0);
}
int[] sum = new int[1];
for (int i=0; i<d[0];i++){
sum[0]+=a[i]*b[i];
}
MPI.COMM_WORLD.Reduce(sum,0,result,0,1,MPI.INT,MPI.SUM,0);
if (me == 0){
System.out.println("answer is"+result[0]+" time of calcs is equal to "+(MPI.Wtime()-startwtime));
}
MPI.Finalize();
}
}
Source: https://habr.com/ru/post/105408/