LCOV - code coverage report
Current view: top level - src/offload - offload_library.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 52.0 % 25 13
Test Date: 2026-11-24 01:22:08 Functions: 54.5 % 11 6

            Line data    Source code
       1              : /*----------------------------------------------------------------------------*/
       2              : /*  CP2K: A general program to perform molecular dynamics simulations         */
       3              : /*  Copyright 2000-2026 CP2K developers group <https://cp2k.org>              */
       4              : /*                                                                            */
       5              : /*  SPDX-License-Identifier: BSD-3-Clause                                     */
       6              : /*----------------------------------------------------------------------------*/
       7              : #include "offload_library.h"
       8              : #include "offload_runtime.h"
       9              : 
      10              : #include <stdint.h>
      11              : #include <stdio.h>
      12              : #include <stdlib.h>
      13              : #include <string.h>
      14              : 
      15              : #if defined(__OFFLOAD_CUDA)
      16              : #include <cuda.h>
      17              : #elif defined(__OFFLOAD_HIP)
      18              : #include <hip/hip_runtime_api.h>
      19              : #elif defined(__OFFLOAD_OPENCL)
      20              : #include <libxstream/libxstream.h>
      21              : #endif
      22              : 
      23              : #if defined(__OFFLOAD_PROFILING)
      24              : #if defined(__OFFLOAD_CUDA)
      25              : #include <nvtx3/nvToolsExt.h>
      26              : #elif defined(__OFFLOAD_HIP) && defined(__HIP_PLATFORM_AMD__)
      27              : #include <roctracer/roctx.h>
      28              : #endif
      29              : #endif
      30              : 
      31              : static int chosen_device_id = -1;
      32              : 
      33              : const uint32_t colormap[] = {0xFFFFFF00,  // Yellow
      34              :                              0xFFFF00FF,  // Fuchsia
      35              :                              0xFFFF0000,  // Red
      36              :                              0xFFC0C0C0,  // Silver
      37              :                              0xFF808080,  // Gray
      38              :                              0xFF808000,  // Olive
      39              :                              0xFF800080,  // Purple
      40              :                              0xFF800000,  // Maroon
      41              :                              0xFF00FFFF,  // Aqua
      42              :                              0xFF00FF00,  // Lime
      43              :                              0xFF008080,  // Teal
      44              :                              0xFF008000,  // Green
      45              :                              0xFF0000FF,  // Blue
      46              :                              0xFF000080}; // Navy
      47              : 
      48              : /*******************************************************************************
      49              :  * \brief Initialize runtime.
      50              :  * \author Rocco Meli
      51              :  ******************************************************************************/
      52         2988 : void offload_init(void) {
      53              : #if defined(__OFFLOAD_CUDA)
      54              :   CUresult error = cuInit(0);
      55              :   if (error != CUDA_SUCCESS) {
      56              :     fprintf(stderr, "ERROR: %s %d %s %d\n", "cuInit failed with error: ", error,
      57              :             __FILE__, __LINE__);
      58              :     abort();
      59              :   }
      60              : #elif defined(__OFFLOAD_HIP)
      61              :   OFFLOAD_CHECK(hipInit(0));
      62              : #elif defined(__OFFLOAD_OPENCL)
      63              :   OFFLOAD_CHECK(libxstream_init());
      64              : #endif
      65         2988 : }
      66              : 
      67              : /*******************************************************************************
      68              :  * \brief Returns the number of available devices.
      69              :  * \author Ole Schuett
      70              :  ******************************************************************************/
      71        25738 : int offload_get_device_count(void) {
      72        25738 :   int count = 0;
      73              : #if defined(__OFFLOAD_CUDA)
      74              :   OFFLOAD_CHECK(cudaGetDeviceCount(&count));
      75              : #elif defined(__OFFLOAD_HIP)
      76              :   OFFLOAD_CHECK(hipGetDeviceCount(&count));
      77              : #elif defined(__OFFLOAD_OPENCL)
      78              :   OFFLOAD_CHECK(libxstream_device_count(&count));
      79              : #endif
      80        25738 :   return count;
      81              : }
      82              : 
      83              : /*******************************************************************************
      84              :  * \brief Selects the chosen device to be used.
      85              :  * \author Ole Schuett
      86              :  ******************************************************************************/
      87            2 : void offload_set_chosen_device(int device_id) { chosen_device_id = device_id; }
      88              : 
      89              : /*******************************************************************************
      90              :  * \brief Returns the chosen device.
      91              :  * \author Ole Schuett
      92              :  ******************************************************************************/
      93            0 : int offload_get_chosen_device(void) { return chosen_device_id; }
      94              : 
      95              : /*******************************************************************************
      96              :  * \brief Activates the device selected via offload_set_chosen_device()
      97              :  * \author Ole Schuett
      98              :  ******************************************************************************/
      99      2043820 : void offload_activate_chosen_device(void) {
     100              : #if defined(__OFFLOAD_CUDA)
     101              :   OFFLOAD_CHECK(cudaSetDevice(chosen_device_id));
     102              : #elif defined(__OFFLOAD_HIP)
     103              :   OFFLOAD_CHECK(hipSetDevice(chosen_device_id));
     104              : #elif defined(__OFFLOAD_OPENCL)
     105              :   if (chosen_device_id >= 0) {
     106              :     OFFLOAD_CHECK(libxstream_device_set_active(chosen_device_id));
     107              :   }
     108              : #endif
     109      2043820 : }
     110              : 
     111              : /*******************************************************************************
     112              :  * \brief Starts a timing range.
     113              :  * \author Ole Schuett
     114              :  ******************************************************************************/
     115   1956578922 : void offload_timeset(const char *message) {
     116              : #if defined(__OFFLOAD_PROFILING)
     117              : #if defined(__OFFLOAD_CUDA)
     118              :   // colors are picked based on a (very simple) hash value of the message
     119              :   int hash = 0;
     120              :   for (size_t i = 0; i < strlen(message); i++) {
     121              :     hash += i * message[i] * message[i];
     122              :   }
     123              :   nvtxEventAttributes_t eventAttrib = {0};
     124              :   eventAttrib.version = NVTX_VERSION;
     125              :   eventAttrib.size = NVTX_EVENT_ATTRIB_STRUCT_SIZE;
     126              :   eventAttrib.messageType = NVTX_MESSAGE_TYPE_ASCII;
     127              :   eventAttrib.message.ascii = message;
     128              :   eventAttrib.colorType = NVTX_COLOR_ARGB;
     129              :   eventAttrib.color = colormap[hash % 14];
     130              :   eventAttrib.payloadType = NVTX_PAYLOAD_TYPE_INT64;
     131              :   eventAttrib.payload.llValue = 123;
     132              :   eventAttrib.category = 42;
     133              :   nvtxRangePushEx(&eventAttrib);
     134              : #elif defined(__OFFLOAD_HIP) && defined(__HIP_PLATFORM_AMD__)
     135              :   roctxRangePushA(message);
     136              : #endif
     137              : #endif
     138   1956578922 :   (void)message; // mark argument as used
     139   1956578922 : }
     140              : 
     141              : /*******************************************************************************
     142              :  * \brief Ends a timing range.
     143              :  * \author Ole Schuett
     144              :  ******************************************************************************/
     145   1956578922 : void offload_timestop(void) {
     146              : #if defined(__OFFLOAD_PROFILING)
     147              : #if defined(__OFFLOAD_CUDA)
     148              :   nvtxRangePop();
     149              : #elif defined(__OFFLOAD_HIP) && defined(__HIP_PLATFORM_AMD__)
     150              :   roctxRangePop();
     151              : #endif
     152              : #endif
     153   1956578922 : }
     154              : 
     155              : /*******************************************************************************
     156              :  * \brief Gets free and total device memory.
     157              :  * \author Ole Schuett
     158              :  ******************************************************************************/
     159            0 : void offload_mem_info(size_t *free, size_t *total) {
     160              : #if defined(__OFFLOAD_CUDA)
     161              :   OFFLOAD_CHECK(cudaMemGetInfo(free, total));
     162              : #elif defined(__OFFLOAD_HIP)
     163              :   OFFLOAD_CHECK(hipMemGetInfo(free, total));
     164              : #elif defined(__OFFLOAD_OPENCL)
     165              :   OFFLOAD_CHECK(libxstream_mem_info(free, total));
     166              : #else
     167            0 :   *free = *total = 0;
     168              : #endif
     169            0 : }
     170              : 
     171              : /*******************************************************************************
     172              :  * \brief Allocate pinned memory (or simple malloc when there is no gpu)
     173              :  ******************************************************************************/
     174            0 : int offload_host_malloc(void **ptr__, const size_t size__) {
     175              : #if defined(__OFFLOAD)
     176              :   offloadMallocHost(ptr__, size__); /* checked */
     177              :   return offloadSuccess;
     178              : #else
     179            0 :   *ptr__ = malloc(size__);
     180            0 :   return EXIT_SUCCESS;
     181              : #endif
     182              : }
     183              : 
     184              : /*******************************************************************************
     185              :  * \brief free pinned memory (or simple free when there is no gpu)
     186              :  ******************************************************************************/
     187            0 : int offload_host_free(void *ptr__) {
     188              : #if defined(__OFFLOAD)
     189              :   offloadFreeHost(ptr__); /* checked */
     190              :   return offloadSuccess;
     191              : #else
     192            0 :   free(ptr__);
     193            0 :   return EXIT_SUCCESS;
     194              : #endif
     195              : }
     196              : 
     197              : /*******************************************************************************
     198              :  * \brief Blocks until all outstanding device work has completed.
     199              :  * \author Samal Bibek
     200              :  ******************************************************************************/
     201            0 : void offload_device_synchronize(void) {
     202              : #if defined(__OFFLOAD_CUDA)
     203              :   OFFLOAD_CHECK(cudaDeviceSynchronize());
     204              : #elif defined(__OFFLOAD_HIP)
     205              :   OFFLOAD_CHECK(hipDeviceSynchronize());
     206              : #endif
     207            0 : }
     208              : 
     209              : // EOF
        

Generated by: LCOV version 2.0-1