forked from snytav/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpi_shortcut.cxx
executable file
·100 lines (73 loc) · 2.2 KB
/
mpi_shortcut.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* *
* Copyright 1993-2012 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*/
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include "archAPI.h"
int InitMPI(int argc,char *argv[])
{
MPI_Init(&argc,&argv);
}
int sumMPI(int size,double *d_jx,double *d_jy,double *d_jz)
{
double *snd,*rcv,*jx,*jy,*jz;
int i;
jx = (double *)malloc(size*sizeof(double));
jy = (double *)malloc(size*sizeof(double));
jz = (double *)malloc(size*sizeof(double));
int err = MemoryCopy(jx,d_jx,size*sizeof(double),DEVICE_TO_HOST);
int err1 = MemoryCopy(jy,d_jy,size*sizeof(double),DEVICE_TO_HOST);
int err2 = MemoryCopy(jz,d_jz,size*sizeof(double),DEVICE_TO_HOST);
printf("sumMPI: err %d err1 %d err2 %d \n",err,err1,err2);
snd = (double *)malloc(3*size*sizeof(double));
rcv = (double *)malloc(3*size*sizeof(double));
for(i = 0;i < size;i++)
{
snd[i] = jx[i];
snd[i + size] = jy[i];
snd[i + 2*size] = jz[i];
}
MPI_Allreduce(snd,rcv,size,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD);
for(i = 0;i < size;i++)
{
jx[i] = rcv[i];
jy[i] = rcv[i + size];
jz[i] = rcv[i + 2*size];
}
err = MemoryCopy(d_jx,jx,size*sizeof(double),HOST_TO_DEVICE);
err1 = MemoryCopy(d_jy,jy,size*sizeof(double),HOST_TO_DEVICE);
err2 = MemoryCopy(d_jz,jz,size*sizeof(double),HOST_TO_DEVICE);
printf("sumMPI-after: err %d err1 %d err2 %d \n",err,err1,err2);
return 0;
}
int sumMPIenergy(double *e)
{
double snd,rcv;
snd = *e;
MPI_Allreduce(&snd,&rcv,1,MPI_DOUBLE_PRECISION,MPI_SUM,MPI_COMM_WORLD);
*e = rcv;
return 0;
}
int getRank()
{
int rank;
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
return rank;
}
int getSize()
{
int rank;
MPI_Comm_size(MPI_COMM_WORLD,&rank);
return rank;
}
int CloseMPI()
{
MPI_Finalize();
}