Skip to content

Commit 46f1e3e

Browse files
author
Martin D. Weinberg
committed
Reorder cuda initialization of periodic BCs until after cuda initialization of component
1 parent b32d6be commit 46f1e3e

4 files changed

Lines changed: 31 additions & 37 deletions

File tree

src/Cube.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ void * Cube::determine_acceleration_and_potential_thread(void * arg)
388388
auto stepy = std::exp(kfac*y);
389389
auto stepz = std::exp(kfac*z);
390390

391-
// Initial values (note sign change)
391+
// Initial values (note sign change from coefficient accumulation)
392392
auto startx = std::exp(-kfac*(x*nmaxx));
393393
auto starty = std::exp(-kfac*(y*nmaxy));
394394
auto startz = std::exp(-kfac*(z*nmaxz));

src/PeriodicBC.H

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private:
2828
#if HAVE_LIBCUDA==1
2929
//! Cuda implementation
3030
void determine_acceleration_and_potential_cuda();
31-
void cuda_initialize();
31+
bool cuda_initialized = false;
3232
#endif
3333

3434
std::vector<double> offset, L;

src/PeriodicBC.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,6 @@ void PeriodicBC::initialize()
145145
<< std::string(60, '-') << std::endl;
146146
throw std::runtime_error("PeriodicBC::initialize: error parsing YAML");
147147
}
148-
149-
150-
// Cuda initialization
151-
#if HAVE_LIBCUDA==1
152-
if (use_cuda) {
153-
cuda_initialize();
154-
}
155-
#endif
156148
}
157149

158150

src/cudaPeriodicBC.cu

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,45 @@ void testConstantsPeriodicBC(cuFP_t tnow)
104104
cudaBCside[0], cudaBCside[1], cudaBCside[2] );
105105
printf("Offset = %e, %e, %e\n",
106106
cudaBCoffset[0], cudaBCoffset[1], cudaBCoffset[2] );
107-
printf(" BCs = %c, %c, %c\n", cudaBC[0], cudaBC[1], cudaBC[2] );
107+
printf(" BCs = %c, %c, %c\n", cudaBC[0], cudaBC[1], cudaBC[2] );
108108
printf("-------------------------\n");
109109
}
110110

111-
void PeriodicBC::cuda_initialize()
111+
112+
void PeriodicBC::determine_acceleration_and_potential_cuda()
112113
{
113-
cuFP_t vec[3];
114-
for (int k=0; k<3; k++) vec[k] = offset[k];
114+
if (not cuda_initialized) {
115+
cuda_initialized = true;
115116

116-
cuda_safe_call(cudaMemcpyToSymbol(cudaBCoffset, vec, sizeof(cuFP_t)*3,
117-
size_t(0), cudaMemcpyHostToDevice),
118-
__FILE__, __LINE__, "Error copying cudaBCoffset");
119-
120-
for (int k=0; k<3; k++) vec[k] = L[k];
117+
// Copy the offset, side length, and BC type to device constant memory
118+
cuFP_t vec[3];
119+
for (int k=0; k<3; k++) vec[k] = offset[k];
121120

122-
cuda_safe_call(cudaMemcpyToSymbol(cudaBCside, vec, sizeof(cuFP_t)*3,
123-
size_t(0), cudaMemcpyHostToDevice),
124-
__FILE__, __LINE__, "Error copying cudaBCside");
121+
cuda_safe_call(cudaMemcpyToSymbol(cudaBCoffset, vec, sizeof(cuFP_t)*3,
122+
size_t(0), cudaMemcpyHostToDevice),
123+
__FILE__, __LINE__, "Error copying cudaBCoffset");
125124

126-
char h_cudaBC[3];
127-
for (int k=0; k<3; k++) h_cudaBC[k] = bc[k];
128-
129-
cuda_safe_call(cudaMemcpyToSymbol(cudaBC, h_cudaBC, sizeof(char)*3,
130-
size_t(0), cudaMemcpyHostToDevice),
131-
__FILE__, __LINE__, "Error copying cudaBC");
125+
for (int k=0; k<3; k++) vec[k] = L[k];
126+
127+
cuda_safe_call(cudaMemcpyToSymbol(cudaBCside, vec, sizeof(cuFP_t)*3,
128+
size_t(0), cudaMemcpyHostToDevice),
129+
__FILE__, __LINE__, "Error copying cudaBCside");
130+
131+
char h_cudaBC[3];
132+
for (int k=0; k<3; k++) h_cudaBC[k] = bc[k];
133+
134+
cuda_safe_call(cudaMemcpyToSymbol(cudaBC, h_cudaBC, sizeof(char)*3,
135+
size_t(0), cudaMemcpyHostToDevice),
136+
__FILE__, __LINE__, "Error copying cudaBC");
132137

133-
if (myid==0 and VERBOSE>4) {
134-
auto cr = cC->cuStream;
135-
testConstantsPeriodicBC<<<1, 1, 0, cr->stream>>>(tnow);
136-
cudaDeviceSynchronize();
137-
cuda_check_last_error_mpi("cudaDeviceSynchronize", __FILE__, __LINE__, myid);
138+
if (myid==0 and VERBOSE>4) {
139+
auto cr = cC->cuStream;
140+
testConstantsPeriodicBC<<<1, 1, 0, cr->stream>>>(tnow);
141+
cudaDeviceSynchronize();
142+
cuda_check_last_error_mpi("cudaDeviceSynchronize", __FILE__, __LINE__, myid);
143+
}
138144
}
139-
}
140145

141-
142-
void PeriodicBC::determine_acceleration_and_potential_cuda()
143-
{
144146
// Sanity check
145147
//
146148
int nbodies = cC->Number();

0 commit comments

Comments
 (0)