LCOV - code coverage report
Current view: top level - src/offload - offload_buffer.c (source / functions) Hit Total Coverage
Test: CP2K Regtests (git:f515968) Lines: 18 18 100.0 %
Date: 2022-07-03 19:52:34 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*----------------------------------------------------------------------------*/
       2             : /*  CP2K: A general program to perform molecular dynamics simulations         */
       3             : /*  Copyright 2000-2022 CP2K developers group <https://cp2k.org>              */
       4             : /*                                                                            */
       5             : /*  SPDX-License-Identifier: BSD-3-Clause                                     */
       6             : /*----------------------------------------------------------------------------*/
       7             : 
       8             : #include <assert.h>
       9             : #include <stdio.h>
      10             : #include <stdlib.h>
      11             : 
      12             : #include "offload_buffer.h"
      13             : #include "offload_library.h"
      14             : #include "offload_runtime.h"
      15             : /*******************************************************************************
      16             :  * \brief Allocates a buffer of given length, ie. number of elements.
      17             :  * \author Ole Schuett
      18             :  ******************************************************************************/
      19      241610 : void offload_create_buffer(const int length, offload_buffer **buffer) {
      20             : 
      21      241610 :   const size_t requested_size = length * sizeof(double);
      22             : 
      23      241610 :   if (*buffer != NULL) {
      24        9500 :     if ((*buffer)->size >= requested_size) {
      25             :       return; // reuse existing buffer
      26             :     } else {
      27          50 :       offload_free_buffer(*buffer);
      28             :     }
      29             :   }
      30             : 
      31      232160 :   (*buffer) = malloc(sizeof(offload_buffer));
      32      232160 :   (*buffer)->size = requested_size;
      33             : 
      34             : #if defined(__OFFLOAD_CUDA)
      35             :   // With size 0 cudaMallocHost doesn't null the pointer and cudaFreeHost fails.
      36             :   (*buffer)->host_buffer = NULL;
      37             :   offload_activate_chosen_device();
      38             :   OFFLOAD_CHECK(
      39             :       cudaMallocHost((void **)&(*buffer)->host_buffer, requested_size));
      40             :   OFFLOAD_CHECK(cudaMalloc((void **)&(*buffer)->device_buffer, requested_size));
      41             : #elif defined(__OFFLOAD_HIP)
      42             :   // With size 0 cudaMallocHost doesn't null the pointer and cudaFreeHost fails.
      43             :   (*buffer)->host_buffer = NULL;
      44             :   offload_activate_chosen_device();
      45             :   OFFLOAD_CHECK(hipHostMalloc((void **)&(*buffer)->host_buffer, requested_size,
      46             :                               hipHostMallocDefault));
      47             :   OFFLOAD_CHECK(hipMalloc((void **)&(*buffer)->device_buffer, requested_size));
      48             : #else
      49      232160 :   (*buffer)->host_buffer = malloc(requested_size);
      50      232160 :   (*buffer)->device_buffer = NULL;
      51             : #endif
      52      232160 :   return;
      53             : }
      54             : 
      55             : /*******************************************************************************
      56             :  * \brief Deallocate given buffer.
      57             :  * \author Ole Schuett
      58             :  ******************************************************************************/
      59      232160 : void offload_free_buffer(offload_buffer *buffer) {
      60             : 
      61      232160 :   if (buffer == NULL)
      62             :     return;
      63             : 
      64             : #ifdef __OFFLOAD_CUDA
      65             :   OFFLOAD_CHECK(cudaFreeHost(buffer->host_buffer));
      66             :   OFFLOAD_CHECK(cudaFree(buffer->device_buffer));
      67             : #elif defined(__OFFLOAD_HIP)
      68             :   OFFLOAD_CHECK(hipHostFree(buffer->host_buffer));
      69             :   OFFLOAD_CHECK(hipFree(buffer->device_buffer));
      70             : #else
      71      232160 :   free(buffer->host_buffer);
      72             : #endif
      73      232160 :   free(buffer);
      74      232160 :   return;
      75             : }
      76             : 
      77             : /*******************************************************************************
      78             :  * \brief Returns a pointer to the host buffer.
      79             :  * \author Ole Schuett
      80             :  ******************************************************************************/
      81      240756 : double *offload_get_buffer_host_pointer(offload_buffer *buffer) {
      82      240756 :   assert(buffer != NULL);
      83      240756 :   return buffer->host_buffer;
      84             : }
      85             : 
      86             : // EOF

Generated by: LCOV version 1.15