@@ -278,18 +278,21 @@ float GPUbenchmark<chunk_t>::benchmarkSync(void (*kernel)(int, chunk_t*, T...),
278278 T&... args) // run for each chunk
279279{
280280 cudaEvent_t start, stop;
281+ cudaStream_t stream;
282+ GPUCHECK (cudaStreamCreate (&stream));
283+
281284 GPUCHECK (cudaSetDevice (mOptions .deviceId ));
282285 chunk_t * chunkPtr = getPartPtr<chunk_t >(mState .scratchPtr , mState .chunkReservedGB , chunkId);
283286
284287 // Warm up
285- (*kernel)<<<blocks, threads, 0 , 0 >>> (0 , chunkPtr, args...);
288+ (*kernel)<<<blocks, threads, 0 , stream >>> (0 , chunkPtr, args...);
286289
287290 GPUCHECK (cudaEventCreate (&start));
288291 GPUCHECK (cudaEventCreate (&stop));
289292
290293 GPUCHECK (cudaEventRecord (start));
291- for (auto iLaunch{0 }; iLaunch < nLaunches; ++iLaunch) { // Schedule all the requested kernel launches
292- (*kernel)<<<blocks, threads, 0 , 0 >>> (chunkId, chunkPtr, args...); // NOLINT: clang-tidy false-positive
294+ for (auto iLaunch{0 }; iLaunch < nLaunches; ++iLaunch) { // Schedule all the requested kernel launches
295+ (*kernel)<<<blocks, threads, 0 , stream >>> (chunkId, chunkPtr, args...); // NOLINT: clang-tidy false-positive
293296 }
294297 GPUCHECK (cudaEventRecord (stop)); // record checkpoint
295298
@@ -299,46 +302,49 @@ float GPUbenchmark<chunk_t>::benchmarkSync(void (*kernel)(int, chunk_t*, T...),
299302 GPUCHECK (cudaEventDestroy (start));
300303 GPUCHECK (cudaEventDestroy (stop));
301304
305+ GPUCHECK (cudaStreamDestroy (stream));
302306 return milliseconds;
303307}
304308
305309template <class chunk_t >
306310template <typename ... T>
307311std::vector<float > GPUbenchmark<chunk_t >::benchmarkAsync(void (*kernel)(int , chunk_t *, T...),
308- int nStreams , int nLaunches, int blocks, int threads, T&... args)
312+ int nChunks , int nLaunches, int blocks, int threads, T&... args)
309313{
310- std::vector<cudaEvent_t> starts (nStreams), stops (nStreams);
311- // std::vector<cudaStream_t> streams(nStreams);
312- std::vector<float > results (nStreams);
314+ std::vector<cudaEvent_t> starts (nChunks), stops (nChunks);
315+ // std::vector<cudaStream_t> streams(nChunks);
316+ cudaStream_t stream;
317+ std::vector<float > results (nChunks);
313318 GPUCHECK (cudaSetDevice (mOptions .deviceId ));
314- for ( auto iStream{ 0 }; iStream < nStreams; ++iStream) { // one stream per chunk
315- GPUCHECK ( cudaStreamCreate (&(streams. at (iStream))));
316- GPUCHECK (cudaEventCreate (&(starts[iStream ])));
317- GPUCHECK (cudaEventCreate (&(stops[iStream ])));
319+ GPUCHECK ( cudaStreamCreate (& stream));
320+ for ( auto iChunk{ 0 }; iChunk < nChunks; ++iChunk) {
321+ GPUCHECK (cudaEventCreate (&(starts[iChunk ])));
322+ GPUCHECK (cudaEventCreate (&(stops[iChunk ])));
318323 }
319- // Warm up on every stream
320- for (auto iStream{0 }; iStream < nStreams; ++iStream) {
321- chunk_t * chunkPtr = getPartPtr<chunk_t >(mState .scratchPtr , mState .chunkReservedGB , iStream);
322- (*kernel)<<<blocks / mState .getMaxChunks(), threads, 0 , streams[iStream]>>> (iStream, chunkPtr, args...);
324+
325+ // Warm up on every chunk
326+ for (auto iChunk{0 }; iChunk < nChunks; ++iChunk) {
327+ chunk_t * chunkPtr = getPartPtr<chunk_t >(mState .scratchPtr , mState .chunkReservedGB , iChunk);
328+ (*kernel)<<<blocks / mState .getMaxChunks(), threads, 0 , stream>>> (iChunk, chunkPtr, args...);
323329 }
324330
325- for (auto iStream {0 }; iStream < nStreams ; ++iStream ) {
326- chunk_t * chunkPtr = getPartPtr<chunk_t >(mState .scratchPtr , mState .chunkReservedGB , iStream );
327- GPUCHECK (cudaEventRecord (starts[iStream ], streams[iStream] ));
331+ for (auto iChunk {0 }; iChunk < nChunks ; ++iChunk ) {
332+ chunk_t * chunkPtr = getPartPtr<chunk_t >(mState .scratchPtr , mState .chunkReservedGB , iChunk );
333+ GPUCHECK (cudaEventRecord (starts[iChunk ], stream ));
328334 for (auto iLaunch{0 }; iLaunch < nLaunches; ++iLaunch) {
329- (*kernel)<<<blocks / mState .getMaxChunks(), threads, 0 /* , streams[iStream] */ >>> (iStream , chunkPtr, args...);
335+ (*kernel)<<<blocks / mState .getMaxChunks(), threads, 0 , stream >>> (iChunk , chunkPtr, args...);
330336 }
331- GPUCHECK (cudaEventRecord (stops[iStream ], streams[iStream] ));
337+ GPUCHECK (cudaEventRecord (stops[iChunk ], stream ));
332338 }
333339
334- for (auto iStream{0 }; iStream < nStreams; ++iStream) {
335- GPUCHECK (cudaEventSynchronize (stops[iStream]));
336- GPUCHECK (cudaEventElapsedTime (&(results.at (iStream)), starts[iStream], stops[iStream]));
337- GPUCHECK (cudaEventDestroy (starts[iStream]));
338- GPUCHECK (cudaEventDestroy (stops[iStream]));
339- GPUCHECK (cudaStreamDestroy (streams[iStream]));
340+ for (auto iChunk{0 }; iChunk < nChunks; ++iChunk) {
341+ GPUCHECK (cudaEventSynchronize (stops[iChunk]));
342+ GPUCHECK (cudaEventElapsedTime (&(results.at (iChunk)), starts[iChunk], stops[iChunk]));
343+ GPUCHECK (cudaEventDestroy (starts[iChunk]));
344+ GPUCHECK (cudaEventDestroy (stops[iChunk]));
340345 }
341346
347+ GPUCHECK (cudaStreamDestroy (stream));
342348 return results;
343349}
344350
0 commit comments