From: Mark Abraham Date: Mon, 4 Dec 2017 10:56:46 +0000 (+1100) Subject: Fix free_gpu X-Git-Url: http://biod.pnpi.spb.ru/gitweb/?a=commitdiff_plain;h=315f6f8346b0dc5a7701081cfd3a4f16de3d3193;p=alexxy%2Fgromacs.git Fix free_gpu If a device context was not used, CUDA gives an error if we attempt to clear it, so we must avoid clearing it. Refs #2322 Change-Id: I67b8b2d263eaed9c7489a6de6f612b27496cc6c2 --- diff --git a/src/gromacs/gpu_utils/gpu_utils.cu b/src/gromacs/gpu_utils/gpu_utils.cu index 4e22007940..273c3baba1 100644 --- a/src/gromacs/gpu_utils/gpu_utils.cu +++ b/src/gromacs/gpu_utils/gpu_utils.cu @@ -536,6 +536,14 @@ void init_gpu(const gmx::MDLogger &mdlog, void free_gpu(const gmx_device_info_t *deviceInfo) { + // One should only attempt to clear the device context when + // it has been used, but currently the only way to know that a GPU + // device was used is that deviceInfo will be non-null. + if (deviceInfo == nullptr) + { + return; + } + cudaError_t stat; if (debug) @@ -546,12 +554,9 @@ void free_gpu(const gmx_device_info_t *deviceInfo) fprintf(stderr, "Cleaning up context on GPU ID #%d\n", gpuid); } - if (deviceInfo != nullptr) + if (!reset_gpu_application_clocks(deviceInfo)) { - if (!reset_gpu_application_clocks(deviceInfo)) - { - gmx_warning("Failed to reset GPU application clocks on GPU #%d", deviceInfo->id); - } + gmx_warning("Failed to reset GPU application clocks on GPU #%d", deviceInfo->id); } stat = cudaDeviceReset(); diff --git a/src/gromacs/gpu_utils/gpu_utils.h b/src/gromacs/gpu_utils/gpu_utils.h index b1cc07b51f..a3254bd467 100644 --- a/src/gromacs/gpu_utils/gpu_utils.h +++ b/src/gromacs/gpu_utils/gpu_utils.h @@ -125,8 +125,12 @@ void init_gpu(const gmx::MDLogger &GPU_FUNC_ARGUMENT(mdlog), /*! \brief Frees up the CUDA GPU used by the active context at the time of calling. * - * The context is explicitly destroyed and therefore all data uploaded to the GPU - * is lost. This should only be called when none of this data is required anymore. + * If \c deviceInfo is nullptr, then it is understood that no device + * was selected so no context is active to be freed. Otherwise, the + * context is explicitly destroyed and therefore all data uploaded to + * the GPU is lost. This must only be called when none of this data is + * required anymore, because subsequent attempts to free memory + * associated with the context will otherwise fail. * * Calls gmx_warning upon errors. *