-
Notifications
You must be signed in to change notification settings - Fork 143
/
mpi_tfunc.c.bak
105 lines (88 loc) · 3.86 KB
/
mpi_tfunc.c.bak
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
101
102
103
104
105
/* mpi_tfunc.c
*
* Copyright (c) 2015, James A. Ross
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <coprthr_mpi.h>
#include "nbody.h"
#define BPP 4
__kernel void
nbody_thread( void* p )
{
my_args_t* pargs = (my_args_t*)p;
int n = pargs->n;
int cnt = pargs->cnt;
unsigned int s_x, s_y, s_z, s_m;
unsigned int page = 0;
float dt = pargs->dt;
float es = pargs->es;
Particle *particles = pargs->p;
ParticleV *state = pargs->v;
int rank, size, npart, i;
int left, right;
MPI_Status status;
MPI_Init(0,MPI_BUF_SIZE);
MPI_Comm comm = MPI_COMM_THREAD;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &size);
MPI_Cart_shift(comm, 0, 1, &left, &right);
npart = n / size;
void* memfree = coprthr_tls_sbrk(0);
Particle* my_particles = (Particle*)coprthr_tls_sbrk(npart*sizeof(Particle));
ParticleV* my_state = (ParticleV*)coprthr_tls_sbrk(npart*sizeof(ParticleV));
Particle* sendbuf = (Particle*)coprthr_tls_sbrk(npart*sizeof(Particle));
e_dma_copy(my_particles, particles + npart*rank, npart*sizeof(Particle));
e_dma_copy(my_state, state + npart*rank, npart*sizeof(ParticleV));
unsigned int rgba_black = 0x00000000;
unsigned int rgba_white = 0x00ffffff;
while (cnt--) {
for (i=0; i<npart; i++) sendbuf[i] = my_particles[i];
for (i=0; i<size; i++) {
if (i) MPI_Sendrecv_replace(sendbuf, sizeof(Particle)/sizeof(float)*npart, MPI_FLOAT, left, 1, right, 1, comm, &status);
ComputeAccel(my_particles, sendbuf, my_state, npart, es);
}
e_dma_copy(particles + npart*rank, my_particles, npart*sizeof(Particle));
ComputeNewPos(my_particles, my_state, npart, dt);
for(i = 0; i < npart; i++){
s_x = (int) particles[i + npart*rank].x;
s_y = (int) particles[i + npart*rank].y;
if(s_x >= 0 && s_x < pargs->fbinfo.xres_virtual && s_y >= 0 && s_y < pargs->fbinfo.yres_virtual){
e_dma_copy((char *) pargs->fbinfo.smem_start + (s_y * pargs->fbinfo.line_length) + (s_x * BPP), (char *) &rgba_black, 1 * BPP);
}
s_x = (int) my_particles[i].x;
s_y = (int) my_particles[i].y;
if(s_x >= 0 && s_x < pargs->fbinfo.xres_virtual && s_y >= 0 && s_y < pargs->fbinfo.yres_virtual){
e_dma_copy((char *) pargs->fbinfo.smem_start + (s_y * pargs->fbinfo.line_length) + (s_x * BPP), (char *) &rgba_white, 1 * BPP);
}
}
}
e_dma_copy(particles + npart*rank, my_particles, npart*sizeof(Particle));
e_dma_copy(state + npart*rank, my_state, npart*sizeof(ParticleV));
coprthr_tls_brk(memfree);
MPI_Finalize();
}