xref: /libCEED/rust/libceed-sys/c-src/backends/cuda/ceed-cuda-common.c (revision ad75ff5827444f41d1c36185c0dbd657b62817df)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include "ceed-cuda-common.h"
9 
10 #include <ceed.h>
11 #include <ceed/backend.h>
12 #include <cuda_runtime.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 //------------------------------------------------------------------------------
17 // Device information backend init
18 //------------------------------------------------------------------------------
19 int CeedInit_Cuda(Ceed ceed, const char *resource) {
20   Ceed_Cuda  *data;
21   const char *device_spec = strstr(resource, ":device_id=");
22   const int   device_id   = (device_spec) ? atoi(device_spec + 11) : -1;
23   int         current_device_id;
24 
25   CeedCallCuda(ceed, cudaGetDevice(&current_device_id));
26   if (device_id >= 0 && current_device_id != device_id) {
27     CeedCallCuda(ceed, cudaSetDevice(device_id));
28     current_device_id = device_id;
29   }
30 
31   CeedCallBackend(CeedGetData(ceed, &data));
32   data->device_id = current_device_id;
33   CeedCallCuda(ceed, cudaGetDeviceProperties(&data->device_prop, current_device_id));
34   return CEED_ERROR_SUCCESS;
35 }
36 
37 //------------------------------------------------------------------------------
38 // Backend destroy
39 //------------------------------------------------------------------------------
40 int CeedDestroy_Cuda(Ceed ceed) {
41   Ceed_Cuda *data;
42 
43   CeedCallBackend(CeedGetData(ceed, &data));
44   if (data->cublas_handle) CeedCallCublas(ceed, cublasDestroy(data->cublas_handle));
45   CeedCallBackend(CeedFree(&data));
46   return CEED_ERROR_SUCCESS;
47 }
48 
49 //------------------------------------------------------------------------------
50 // Memory transfer utilities
51 //------------------------------------------------------------------------------
52 static inline int CeedSetDeviceGenericArray_Cuda(Ceed ceed, const void *source_array, CeedCopyMode copy_mode, size_t size_unit, CeedSize num_values,
53                                                  void *target_array_owned, void *target_array_borrowed, void *target_array) {
54   switch (copy_mode) {
55     case CEED_COPY_VALUES:
56       if (!*(void **)target_array_owned) CeedCallCuda(ceed, cudaMalloc(target_array_owned, size_unit * num_values));
57       if (source_array) CeedCallCuda(ceed, cudaMemcpy(*(void **)target_array_owned, source_array, size_unit * num_values, cudaMemcpyDeviceToDevice));
58       *(void **)target_array_borrowed = NULL;
59       *(void **)target_array          = *(void **)target_array_owned;
60       break;
61     case CEED_OWN_POINTER:
62       CeedCallCuda(ceed, cudaFree(*(void **)target_array_owned));
63       *(void **)target_array_owned    = (void *)source_array;
64       *(void **)target_array_borrowed = NULL;
65       *(void **)target_array          = *(void **)target_array_owned;
66       break;
67     case CEED_USE_POINTER:
68       CeedCallCuda(ceed, cudaFree(*(void **)target_array_owned));
69       *(void **)target_array_owned    = NULL;
70       *(void **)target_array_borrowed = (void *)source_array;
71       *(void **)target_array          = *(void **)target_array_borrowed;
72   }
73   return CEED_ERROR_SUCCESS;
74 }
75 
76 int CeedSetDeviceBoolArray_Cuda(Ceed ceed, const bool *source_array, CeedCopyMode copy_mode, CeedSize num_values, const bool **target_array_owned,
77                                 const bool **target_array_borrowed, const bool **target_array) {
78   CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(bool), num_values, target_array_owned, target_array_borrowed,
79                                                  target_array));
80   return CEED_ERROR_SUCCESS;
81 }
82 
83 int CeedSetDeviceCeedInt8Array_Cuda(Ceed ceed, const CeedInt8 *source_array, CeedCopyMode copy_mode, CeedSize num_values,
84                                     const CeedInt8 **target_array_owned, const CeedInt8 **target_array_borrowed, const CeedInt8 **target_array) {
85   CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedInt8), num_values, target_array_owned,
86                                                  target_array_borrowed, target_array));
87   return CEED_ERROR_SUCCESS;
88 }
89 
90 int CeedSetDeviceCeedIntArray_Cuda(Ceed ceed, const CeedInt *source_array, CeedCopyMode copy_mode, CeedSize num_values,
91                                    const CeedInt **target_array_owned, const CeedInt **target_array_borrowed, const CeedInt **target_array) {
92   CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedInt), num_values, target_array_owned,
93                                                  target_array_borrowed, target_array));
94   return CEED_ERROR_SUCCESS;
95 }
96 
97 int CeedSetDeviceCeedScalarArray_Cuda(Ceed ceed, const CeedScalar *source_array, CeedCopyMode copy_mode, CeedSize num_values,
98                                       const CeedScalar **target_array_owned, const CeedScalar **target_array_borrowed,
99                                       const CeedScalar **target_array) {
100   CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedScalar), num_values, target_array_owned,
101                                                  target_array_borrowed, target_array));
102   return CEED_ERROR_SUCCESS;
103 }
104 
105 //------------------------------------------------------------------------------
106