1 // Copyright (c) 2017-2026, 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 //------------------------------------------------------------------------------
CeedInit_Cuda(Ceed ceed,const char * resource)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(¤t_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 //------------------------------------------------------------------------------
CeedDestroy_Cuda(Ceed ceed)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 //------------------------------------------------------------------------------
CeedSetDeviceGenericArray_Cuda(Ceed ceed,const void * source_array,CeedCopyMode copy_mode,size_t size_unit,CeedSize num_values,void * target_array_owned,void * target_array_borrowed,void * target_array)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) {
57 if (*(void **)target_array_borrowed) {
58 *(void **)target_array = *(void **)target_array_borrowed;
59 } else {
60 if (!*(void **)target_array_owned) CeedCallCuda(ceed, cudaMalloc(target_array_owned, size_unit * num_values));
61 *(void **)target_array = *(void **)target_array_owned;
62 }
63 }
64 if (source_array) CeedCallCuda(ceed, cudaMemcpy(*(void **)target_array, source_array, size_unit * num_values, cudaMemcpyDeviceToDevice));
65 break;
66 case CEED_OWN_POINTER:
67 CeedCallCuda(ceed, cudaFree(*(void **)target_array_owned));
68 *(void **)target_array_owned = (void *)source_array;
69 *(void **)target_array_borrowed = NULL;
70 *(void **)target_array = *(void **)target_array_owned;
71 break;
72 case CEED_USE_POINTER:
73 CeedCallCuda(ceed, cudaFree(*(void **)target_array_owned));
74 *(void **)target_array_owned = NULL;
75 *(void **)target_array_borrowed = (void *)source_array;
76 *(void **)target_array = *(void **)target_array_borrowed;
77 }
78 return CEED_ERROR_SUCCESS;
79 }
80
CeedSetDeviceBoolArray_Cuda(Ceed ceed,const bool * source_array,CeedCopyMode copy_mode,CeedSize num_values,const bool ** target_array_owned,const bool ** target_array_borrowed,const bool ** target_array)81 int CeedSetDeviceBoolArray_Cuda(Ceed ceed, const bool *source_array, CeedCopyMode copy_mode, CeedSize num_values, const bool **target_array_owned,
82 const bool **target_array_borrowed, const bool **target_array) {
83 CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(bool), num_values, target_array_owned, target_array_borrowed,
84 target_array));
85 return CEED_ERROR_SUCCESS;
86 }
87
CeedSetDeviceCeedInt8Array_Cuda(Ceed ceed,const CeedInt8 * source_array,CeedCopyMode copy_mode,CeedSize num_values,const CeedInt8 ** target_array_owned,const CeedInt8 ** target_array_borrowed,const CeedInt8 ** target_array)88 int CeedSetDeviceCeedInt8Array_Cuda(Ceed ceed, const CeedInt8 *source_array, CeedCopyMode copy_mode, CeedSize num_values,
89 const CeedInt8 **target_array_owned, const CeedInt8 **target_array_borrowed, const CeedInt8 **target_array) {
90 CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedInt8), num_values, target_array_owned,
91 target_array_borrowed, target_array));
92 return CEED_ERROR_SUCCESS;
93 }
94
CeedSetDeviceCeedIntArray_Cuda(Ceed ceed,const CeedInt * source_array,CeedCopyMode copy_mode,CeedSize num_values,const CeedInt ** target_array_owned,const CeedInt ** target_array_borrowed,const CeedInt ** target_array)95 int CeedSetDeviceCeedIntArray_Cuda(Ceed ceed, const CeedInt *source_array, CeedCopyMode copy_mode, CeedSize num_values,
96 const CeedInt **target_array_owned, const CeedInt **target_array_borrowed, const CeedInt **target_array) {
97 CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedInt), num_values, target_array_owned,
98 target_array_borrowed, target_array));
99 return CEED_ERROR_SUCCESS;
100 }
101
CeedSetDeviceCeedScalarArray_Cuda(Ceed ceed,const CeedScalar * source_array,CeedCopyMode copy_mode,CeedSize num_values,const CeedScalar ** target_array_owned,const CeedScalar ** target_array_borrowed,const CeedScalar ** target_array)102 int CeedSetDeviceCeedScalarArray_Cuda(Ceed ceed, const CeedScalar *source_array, CeedCopyMode copy_mode, CeedSize num_values,
103 const CeedScalar **target_array_owned, const CeedScalar **target_array_borrowed,
104 const CeedScalar **target_array) {
105 CeedCallBackend(CeedSetDeviceGenericArray_Cuda(ceed, source_array, copy_mode, sizeof(CeedScalar), num_values, target_array_owned,
106 target_array_borrowed, target_array));
107 return CEED_ERROR_SUCCESS;
108 }
109
110 //------------------------------------------------------------------------------
111