-
Notifications
You must be signed in to change notification settings - Fork 29
/
psm_user.h
657 lines (555 loc) · 20.9 KB
/
psm_user.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
/*
This file is provided under a dual BSD/GPLv2 license. When using or
redistributing this file, you may do so under either license.
GPL LICENSE SUMMARY
Copyright(c) 2021 Cornelis Networks.
Copyright(c) 2016 Intel Corporation.
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Contact Information:
Cornelis Networks, www.cornelisnetworks.com
BSD LICENSE
Copyright(c) 2021 Cornelis Networks.
Copyright(c) 2016 Intel Corporation.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* Copyright (c) 2003-2016 Intel Corporation. All rights reserved. */
#ifndef _PSMI_USER_H
#define _PSMI_USER_H
#ifdef __cplusplus
extern "C" {
#endif
#include "psm_config.h"
#include <inttypes.h>
#include <pthread.h>
#include <sched.h>
#include <numa.h>
#include <semaphore.h>
#include <fcntl.h>
#include <stdbool.h>
#include "psm2.h"
#include "psm2_mq.h"
#include "ptl.h"
#include "opa_user.h"
#include "opa_queue.h"
#include "psm_log.h"
#include "psm_perf.h"
#define PSMI_LOCK_NO_OWNER ((pthread_t)(-1))
#define _PSMI_IN_USER_H
/* Opaque hw context pointer used in HAL,
and defined by each HAL instance. */
typedef void *psmi_hal_hw_context;
#include "psm_help.h"
#include "psm_error.h"
#include "psm_context.h"
#include "psm_utils.h"
#include "psm_timer.h"
#include "psm_mpool.h"
#include "psm_ep.h"
#include "psm_lock.h"
#include "psm_stats.h"
#include "psm2_mock_testing.h"
#undef _PSMI_IN_USER_H
#define PSMI_VERNO_MAKE(major, minor) ((((major)&0xff)<<8)|((minor)&0xff))
#define PSMI_VERNO PSMI_VERNO_MAKE(PSM2_VERNO_MAJOR, PSM2_VERNO_MINOR)
#define PSMI_VERNO_GET_MAJOR(verno) (((verno)>>8) & 0xff)
#define PSMI_VERNO_GET_MINOR(verno) (((verno)>>0) & 0xff)
int psmi_verno_client();
int psmi_verno_isinteroperable(uint16_t verno);
int MOCKABLE(psmi_isinitialized)();
MOCK_DCL_EPILOGUE(psmi_isinitialized);
psm2_error_t psmi_poll_internal(psm2_ep_t ep, int poll_amsh);
psm2_error_t psmi_mq_wait_internal(psm2_mq_req_t *ireq);
int psmi_get_current_proc_location();
extern int psmi_epid_ver;
extern uint32_t non_dw_mul_sdma;
extern psmi_lock_t psmi_creation_lock;
extern psm2_ep_t psmi_opened_endpoint;
extern int psmi_affinity_shared_file_opened;
extern uint64_t *shared_affinity_ptr;
extern char *affinity_shm_name;
extern sem_t *sem_affinity_shm_rw;
extern int psmi_affinity_semaphore_open;
extern char *sem_affinity_shm_rw_name;
PSMI_ALWAYS_INLINE(
int
_psmi_get_epid_version()) {
return psmi_epid_ver;
}
#define PSMI_EPID_VERSION_SHM 0
#define PSMI_EPID_SHM_ONLY 1
#define PSMI_EPID_IPS_SHM 0
#define PSMI_EPID_VERSION _psmi_get_epid_version()
#define PSMI_MAX_EPID_VERNO_SUPPORTED 2
#define PSMI_MIN_EPID_VERNO_SUPPORTED 1
#define PSMI_EPID_VERNO_DEFAULT 2
#define PSMI_EPID_V1 1
#define PSMI_EPID_V2 2
#define PSMI_EPID_GET_LID(epid) (PSMI_EPID_VERSION == PSMI_EPID_V1) ? \
(int)PSMI_EPID_GET_LID_V1(epid) \
: (int)PSMI_EPID_GET_LID_V2(epid)
#define PSMI_GET_SUBNET_ID(gid_hi) (gid_hi & 0xffff)
/*
* Following is the definition of various lock implementations. The choice is
* made by defining specific lock type in relevant section of psm_config.h
*/
#ifdef PSMI_LOCK_IS_SPINLOCK
#define _PSMI_LOCK_INIT(pl) psmi_spin_init(&((pl).lock))
#define _PSMI_LOCK_TRY(pl) psmi_spin_trylock(&((pl).lock))
#define _PSMI_LOCK(pl) psmi_spin_lock(&((pl).lock))
#define _PSMI_UNLOCK(pl) psmi_spin_unlock(&((pl).lock))
#define _PSMI_LOCK_ASSERT(pl)
#define _PSMI_UNLOCK_ASSERT(pl)
#define PSMI_LOCK_DISABLED 0
#elif defined(PSMI_LOCK_IS_MUTEXLOCK_DEBUG)
PSMI_ALWAYS_INLINE(
int
_psmi_mutex_trylock_inner(pthread_mutex_t *mutex,
const char *curloc, pthread_t *lock_owner))
{
psmi_assert_always_loc(*lock_owner != pthread_self(),
curloc);
int ret = pthread_mutex_trylock(mutex);
if (ret == 0)
*lock_owner = pthread_self();
return ret;
}
PSMI_ALWAYS_INLINE(
int
_psmi_mutex_lock_inner(pthread_mutex_t *mutex,
const char *curloc, pthread_t *lock_owner))
{
psmi_assert_always_loc(*lock_owner != pthread_self(),
curloc);
int ret = pthread_mutex_lock(mutex);
psmi_assert_always_loc(ret != EDEADLK, curloc);
*lock_owner = pthread_self();
return ret;
}
PSMI_ALWAYS_INLINE(
void
_psmi_mutex_unlock_inner(pthread_mutex_t *mutex,
const char *curloc, pthread_t *lock_owner))
{
psmi_assert_always_loc(*lock_owner == pthread_self(),
curloc);
*lock_owner = PSMI_LOCK_NO_OWNER;
psmi_assert_always_loc(pthread_mutex_unlock(mutex) !=
EPERM, curloc);
return;
}
#define _PSMI_LOCK_INIT(pl) /* static initialization */
#define _PSMI_LOCK_TRY(pl) \
_psmi_mutex_trylock_inner(&((pl).lock), PSMI_CURLOC, \
&((pl).lock_owner))
#define _PSMI_LOCK(pl) \
_psmi_mutex_lock_inner(&((pl).lock), PSMI_CURLOC, \
&((pl).lock_owner))
#define _PSMI_UNLOCK(pl) \
_psmi_mutex_unlock_inner(&((pl).lock), PSMI_CURLOC, \
&((pl).lock_owner))
#define _PSMI_LOCK_ASSERT(pl) \
psmi_assert_always((pl).lock_owner == pthread_self());
#define _PSMI_UNLOCK_ASSERT(pl) \
psmi_assert_always((pl).lock_owner != pthread_self());
#define PSMI_LOCK_DISABLED 0
#elif defined(PSMI_LOCK_IS_MUTEXLOCK)
#define _PSMI_LOCK_INIT(pl) /* static initialization */
#define _PSMI_LOCK_TRY(pl) pthread_mutex_trylock(&((pl).lock))
#define _PSMI_LOCK(pl) pthread_mutex_lock(&((pl).lock))
#define _PSMI_UNLOCK(pl) pthread_mutex_unlock(&((pl).lock))
#define PSMI_LOCK_DISABLED 0
#define _PSMI_LOCK_ASSERT(pl)
#define _PSMI_UNLOCK_ASSERT(pl)
#elif defined(PSMI_PLOCK_IS_NOLOCK)
#define _PSMI_LOCK_TRY(pl) 0 /* 0 *only* so progress thread never succeeds */
#define _PSMI_LOCK(pl)
#define _PSMI_UNLOCK(pl)
#define PSMI_LOCK_DISABLED 1
#define _PSMI_LOCK_ASSERT(pl)
#define _PSMI_UNLOCK_ASSERT(pl)
#else
#error No LOCK lock type declared
#endif
#define PSMI_YIELD(pl) \
do { _PSMI_UNLOCK((pl)); sched_yield(); _PSMI_LOCK((pl)); } while (0)
#ifdef PSM2_MOCK_TESTING
/* If this is a mocking tests build, all the operations on the locks
* are routed through functions which may be mocked, if necessary. */
void MOCKABLE(psmi_mockable_lock_init)(psmi_lock_t *pl);
MOCK_DCL_EPILOGUE(psmi_mockable_lock_init);
int MOCKABLE(psmi_mockable_lock_try)(psmi_lock_t *pl);
MOCK_DCL_EPILOGUE(psmi_mockable_lock_try);
void MOCKABLE(psmi_mockable_lock)(psmi_lock_t *pl);
MOCK_DCL_EPILOGUE(psmi_mockable_lock);
void MOCKABLE(psmi_mockable_unlock)(psmi_lock_t *pl);
MOCK_DCL_EPILOGUE(psmi_mockable_unlock);
void MOCKABLE(psmi_mockable_lock_assert)(psmi_lock_t *pl);
MOCK_DCL_EPILOGUE(psmi_mockable_lock_assert);
void MOCKABLE(psmi_mockable_unlock_assert)(psmi_lock_t *pl);
MOCK_DCL_EPILOGUE(psmi_mockable_unlock_assert);
#define PSMI_LOCK_INIT(pl) psmi_mockable_lock_init(&(pl))
#define PSMI_LOCK_TRY(pl) psmi_mockable_lock_try(&(pl))
#define PSMI_LOCK(pl) psmi_mockable_lock(&(pl))
#define PSMI_UNLOCK(pl) psmi_mockable_unlock(&(pl))
#define PSMI_LOCK_ASSERT(pl) psmi_mockable_lock_assert(&(pl))
#define PSMI_UNLOCK_ASSERT(pl) psmi_mockable_unlock_assert(&(pl))
#else
#define PSMI_LOCK_INIT(pl) _PSMI_LOCK_INIT(pl)
#define PSMI_LOCK_TRY(pl) _PSMI_LOCK_TRY(pl)
#define PSMI_LOCK(pl) _PSMI_LOCK(pl)
#define PSMI_UNLOCK(pl) _PSMI_UNLOCK(pl)
#define PSMI_LOCK_ASSERT(pl) _PSMI_LOCK_ASSERT(pl)
#define PSMI_UNLOCK_ASSERT(pl) _PSMI_UNLOCK_ASSERT(pl)
#endif
#ifdef PSM_PROFILE
void psmi_profile_block() __attribute__ ((weak));
void psmi_profile_unblock() __attribute__ ((weak));
void psmi_profile_reblock(int did_no_progress) __attribute__ ((weak));
#define PSMI_PROFILE_BLOCK() psmi_profile_block()
#define PSMI_PROFILE_UNBLOCK() psmi_profile_unblock()
#define PSMI_PROFILE_REBLOCK(noprog) psmi_profile_reblock(noprog)
#else
#define PSMI_PROFILE_BLOCK()
#define PSMI_PROFILE_UNBLOCK()
#define PSMI_PROFILE_REBLOCK(noprog)
#endif
#ifdef PSM_CUDA
#include <cuda.h>
#include <driver_types.h>
#if CUDA_VERSION < 7000
#error Please update CUDA driver, required minimum version is 7.0
#endif
extern int is_cuda_enabled;
extern int is_gdr_copy_enabled;
extern int _device_support_gpudirect;
extern int _gpu_p2p_supported;
extern int my_gpu_device;
extern int cuda_lib_version;
extern CUcontext cu_ctxt;
extern void *psmi_cuda_lib;
extern CUresult (*psmi_cuInit)(unsigned int Flags );
extern CUresult (*psmi_cuCtxDetach)(CUcontext c);
extern CUresult (*psmi_cuCtxGetCurrent)(CUcontext *c);
extern CUresult (*psmi_cuCtxSetCurrent)(CUcontext c);
extern CUresult (*psmi_cuPointerGetAttribute)(void *data, CUpointer_attribute pa, CUdeviceptr p);
extern CUresult (*psmi_cuPointerSetAttribute)(void *data, CUpointer_attribute pa, CUdeviceptr p);
extern CUresult (*psmi_cuDeviceCanAccessPeer)(int *canAccessPeer, CUdevice dev, CUdevice peerDev);
extern CUresult (*psmi_cuDeviceGet)(CUdevice* device, int ordinal);
extern CUresult (*psmi_cuDeviceGetAttribute)(int* pi, CUdevice_attribute attrib, CUdevice dev);
extern CUresult (*psmi_cuDriverGetVersion)(int* driverVersion);
extern CUresult (*psmi_cuDeviceGetCount)(int* count);
extern CUresult (*psmi_cuStreamCreate)(CUstream* phStream, unsigned int Flags);
extern CUresult (*psmi_cuStreamDestroy)(CUstream phStream);
extern CUresult (*psmi_cuStreamSynchronize)(CUstream phStream);
extern CUresult (*psmi_cuEventCreate)(CUevent* phEvent, unsigned int Flags);
extern CUresult (*psmi_cuEventDestroy)(CUevent hEvent);
extern CUresult (*psmi_cuEventQuery)(CUevent hEvent);
extern CUresult (*psmi_cuEventRecord)(CUevent hEvent, CUstream hStream);
extern CUresult (*psmi_cuEventSynchronize)(CUevent hEvent);
extern CUresult (*psmi_cuMemHostAlloc)(void** pp, size_t bytesize, unsigned int Flags);
extern CUresult (*psmi_cuMemFreeHost)(void* p);
extern CUresult (*psmi_cuMemcpy)(CUdeviceptr dst, CUdeviceptr src, size_t ByteCount);
extern CUresult (*psmi_cuMemcpyDtoD)(CUdeviceptr dstDevice, CUdeviceptr srcDevice, size_t ByteCount);
extern CUresult (*psmi_cuMemcpyDtoH)(void* dstHost, CUdeviceptr srcDevice, size_t ByteCount);
extern CUresult (*psmi_cuMemcpyHtoD)(CUdeviceptr dstDevice, const void* srcHost, size_t ByteCount);
extern CUresult (*psmi_cuMemcpyDtoHAsync)(void* dstHost, CUdeviceptr srcDevice, size_t ByteCount, CUstream hStream);
extern CUresult (*psmi_cuMemcpyHtoDAsync)(CUdeviceptr dstDevice, const void* srcHost, size_t ByteCount, CUstream hStream);
extern CUresult (*psmi_cuIpcGetMemHandle)(CUipcMemHandle* pHandle, CUdeviceptr dptr);
extern CUresult (*psmi_cuIpcOpenMemHandle)(CUdeviceptr* pdptr, CUipcMemHandle handle, unsigned int Flags);
extern CUresult (*psmi_cuIpcCloseMemHandle)(CUdeviceptr dptr);
extern CUresult (*psmi_cuMemGetAddressRange)(CUdeviceptr* pbase, size_t* psize, CUdeviceptr dptr);
extern CUresult (*psmi_cuDevicePrimaryCtxGetState)(CUdevice dev, unsigned int* flags, int* active);
extern CUresult (*psmi_cuDevicePrimaryCtxRetain)(CUcontext* pctx, CUdevice dev);
extern CUresult (*psmi_cuCtxGetDevice)(CUdevice* device);
extern CUresult (*psmi_cuDevicePrimaryCtxRelease)(CUdevice device);
static int check_set_cuda_ctxt(void)
{
CUresult err;
CUcontext tmpctxt = {0};
if (!psmi_cuCtxGetCurrent || !psmi_cuCtxSetCurrent)
return 0;
err = psmi_cuCtxGetCurrent(&tmpctxt);
if (!err) {
if (!tmpctxt && cu_ctxt) {
err = psmi_cuCtxSetCurrent(cu_ctxt);
return !!err;
} else if (tmpctxt && !cu_ctxt) {
cu_ctxt = tmpctxt;
}
}
return 0;
}
#define PSMI_CUDA_CALL(func, args...) do { \
_HFI_CUDADBG("func=psmi_"#func"\n"); \
CUresult cudaerr; \
if (check_set_cuda_ctxt()) { \
psmi_handle_error(PSMI_EP_NORETURN, PSM2_INTERNAL_ERR, \
"Failed to set/synchronize CUDA context.\n"); \
} \
cudaerr = psmi_##func(args); \
if (cudaerr != CUDA_SUCCESS) { \
_HFI_ERROR( \
"CUDA failure: %s() (at %s:%d)" \
"returned %d\n", \
#func, __FILE__, __LINE__, cudaerr); \
psmi_handle_error( \
PSMI_EP_NORETURN, PSM2_INTERNAL_ERR, \
"Error returned from CUDA function.\n");\
} \
} while (0)
PSMI_ALWAYS_INLINE(
int device_support_gpudirect())
{
if (_device_support_gpudirect > -1) return _device_support_gpudirect;
int num_devices, dev;
/* Check if all devices support Unified Virtual Addressing. */
PSMI_CUDA_CALL(cuDeviceGetCount, &num_devices);
_device_support_gpudirect = 1;
for (dev = 0; dev < num_devices; dev++) {
CUdevice device;
PSMI_CUDA_CALL(cuDeviceGet, &device, dev);
int unifiedAddressing;
PSMI_CUDA_CALL(cuDeviceGetAttribute,
&unifiedAddressing,
CU_DEVICE_ATTRIBUTE_UNIFIED_ADDRESSING,
device);
if (unifiedAddressing !=1) {
psmi_handle_error(PSMI_EP_NORETURN, PSM2_EP_DEVICE_FAILURE,
"CUDA device %d does not support Unified Virtual Addressing.\n",
dev);
}
int major;
PSMI_CUDA_CALL(cuDeviceGetAttribute,
&major,
CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR,
device);
if (major < 3) {
_device_support_gpudirect = 0;
_HFI_INFO("CUDA device %d does not support GPUDirect RDMA (Non-fatal error)\n", dev);
}
}
return _device_support_gpudirect;
}
#define PSMI_IS_CUDA_ENABLED likely(is_cuda_enabled)
#define PSMI_IS_CUDA_DISABLED unlikely(!is_cuda_enabled)
PSMI_ALWAYS_INLINE(
int gpu_p2p_supported())
{
if (likely(_gpu_p2p_supported > -1)) return _gpu_p2p_supported;
if (PSMI_IS_CUDA_DISABLED) {
_gpu_p2p_supported=0;
return 0;
}
int num_devices, dev;
/* Check which devices the current device has p2p access to. */
CUdevice current_device;
PSMI_CUDA_CALL(cuCtxGetDevice, ¤t_device);
PSMI_CUDA_CALL(cuDeviceGetCount, &num_devices);
_gpu_p2p_supported = 0;
for (dev = 0; dev < num_devices; dev++) {
CUdevice device;
PSMI_CUDA_CALL(cuDeviceGet, &device, dev);
if (device != current_device) {
int canAccessPeer = 0;
PSMI_CUDA_CALL(cuDeviceCanAccessPeer, &canAccessPeer,
current_device, device);
if (canAccessPeer != 1)
_HFI_DBG("CUDA device %d does not support P2P from current device (Non-fatal error)\n", dev);
else
_gpu_p2p_supported |= (1 << device);
} else {
/* Always support p2p on the same GPU */
my_gpu_device = device;
_gpu_p2p_supported |= (1 << device);
}
}
return _gpu_p2p_supported;
}
/**
* Similar to PSMI_CUDA_CALL() except does not error out
* if func(args) returns CUDA_SUCCESS or except_err
*
* Invoker must provide 'CUresult cudaerr' in invoked scope
* so invoker can inspect whether cudaerr == CUDA_SUCCESS or
* cudaerr == except_err after expanded code is executed.
*
* As except_err is an allowed value, message is printed at
* DBG level.
*/
#define PSMI_CUDA_CALL_EXCEPT(except_err, func, args...) do { \
_HFI_CUDADBG("func=psmi_"#func",except_err=%d\n", except_err); \
if (check_set_cuda_ctxt()) { \
psmi_handle_error(PSMI_EP_NORETURN, PSM2_INTERNAL_ERR, \
"Failed to set/synchronize CUDA context.\n"); \
} \
cudaerr = psmi_##func(args); \
if (cudaerr != CUDA_SUCCESS && cudaerr != except_err) { \
if (cu_ctxt == NULL) \
_HFI_ERROR( \
"Check if CUDA is initialized" \
"before psm2_ep_open call \n"); \
_HFI_ERROR( \
"CUDA failure: %s() (at %s:%d)" \
"returned %d\n", \
#func, __FILE__, __LINE__, cudaerr); \
psmi_handle_error( \
PSMI_EP_NORETURN, PSM2_INTERNAL_ERR, \
"Error returned from CUDA function.\n");\
} else if (cudaerr == except_err) { \
_HFI_DBG( \
"CUDA non-zero return value: %s() (at %s:%d)" \
"returned %d\n", \
#func, __FILE__, __LINE__, cudaerr); \
} \
} while (0)
#define PSMI_CUDA_CHECK_EVENT(event, cudaerr) do { \
_HFI_CUDADBG("event=%p\n", (void*)(event)); \
cudaerr = psmi_cuEventQuery(event); \
if ((cudaerr != CUDA_SUCCESS) && \
(cudaerr != CUDA_ERROR_NOT_READY)) { \
_HFI_ERROR( \
"CUDA failure: %s() returned %d\n", \
"cuEventQuery", cudaerr); \
psmi_handle_error( \
PSMI_EP_NORETURN, PSM2_INTERNAL_ERR, \
"Error returned from CUDA function.\n");\
} \
} while (0)
#define PSMI_CUDA_DLSYM(psmi_cuda_lib,func) do { \
psmi_##func = dlsym(psmi_cuda_lib, STRINGIFY(func)); \
if (!psmi_##func) { \
psmi_handle_error(PSMI_EP_NORETURN, \
PSM2_INTERNAL_ERR, \
" Unable to resolve %s symbol" \
" in CUDA libraries.\n",STRINGIFY(func));\
} \
} while (0)
PSMI_ALWAYS_INLINE(
int
_psmi_is_cuda_mem(const void *ptr))
{
CUresult cres;
CUmemorytype mt;
unsigned uvm = 0;
cres = psmi_cuPointerGetAttribute(
&mt, CU_POINTER_ATTRIBUTE_MEMORY_TYPE, (CUdeviceptr) ptr);
if ((cres == CUDA_SUCCESS) && (mt == CU_MEMORYTYPE_DEVICE)) {
cres = psmi_cuPointerGetAttribute(
&uvm, CU_POINTER_ATTRIBUTE_IS_MANAGED, (CUdeviceptr) ptr);
if ((cres == CUDA_SUCCESS) && (uvm == 0))
return 1;
else
return 0;
} else
return 0;
}
PSMI_ALWAYS_INLINE(
int
_psmi_is_gdr_copy_enabled())
{
return is_gdr_copy_enabled;
}
#define PSMI_IS_GDR_COPY_ENABLED _psmi_is_gdr_copy_enabled()
#define PSMI_IS_CUDA_MEM(p) _psmi_is_cuda_mem(p)
struct ips_cuda_hostbuf {
STAILQ_ENTRY(ips_cuda_hostbuf) req_next;
STAILQ_ENTRY(ips_cuda_hostbuf) next;
uint32_t size, offset, bytes_read;
/* This flag indicates whether a chb is
* pulled from a mpool or dynamically
* allocated using calloc. */
uint8_t is_tempbuf;
CUevent copy_status;
psm2_mq_req_t req;
void *host_buf;
CUdeviceptr gpu_buf;
};
struct ips_cuda_hostbuf_mpool_cb_context {
unsigned bufsz;
};
void psmi_cuda_hostbuf_alloc_func(int is_alloc, void *obj);
#define CUDA_HOSTBUFFER_LIMITS { \
.env = "PSM_CUDA_BOUNCEBUFFERS_MAX", \
.descr = "Max CUDA bounce buffers (in MB)", \
.env_level = PSMI_ENVVAR_LEVEL_HIDDEN, \
.minval = 1, \
.maxval = 1<<30, \
.mode[PSMI_MEMMODE_NORMAL] = { 16, 256 }, \
.mode[PSMI_MEMMODE_MINIMAL] = { 1, 1 }, \
.mode[PSMI_MEMMODE_LARGE] = { 32, 512 } \
}
extern uint32_t gpudirect_send_threshold;
extern uint32_t gpudirect_recv_threshold;
extern uint32_t cuda_thresh_rndv;
/* This threshold dictates when the sender turns off
* GDR Copy. The threshold needs to be less than
* CUDA RNDV threshold.
*/
extern uint32_t gdr_copy_threshold_send;
/* This threshold dictates when the reciever turns off
* GDR Copy. The threshold needs to be less than
* CUDA RNDV threshold.
*/
extern uint32_t gdr_copy_threshold_recv;
#define PSMI_USE_GDR_COPY(req, len) \
req->is_buf_gpu_mem && \
PSMI_IS_GDR_COPY_ENABLED && \
(req->flags_user & PSM2_MQ_FLAG_GDRCPY_ONLY || (len >=1 && len <= gdr_copy_threshold_recv))
enum psm2_chb_match_type {
/* Complete data found in a single chb */
PSMI_CUDA_FULL_MATCH_FOUND = 0,
/* Data is spread across two chb's */
PSMI_CUDA_SPLIT_MATCH_FOUND = 1,
/* Data is only partially prefetched */
PSMI_CUDA_PARTIAL_MATCH_FOUND = 2,
PSMI_CUDA_CONTINUE = 3
};
typedef enum psm2_chb_match_type psm2_chb_match_type_t;
/*
* CUDA documentation dictates the use of SYNC_MEMOPS attribute
* when the buffer pointer received into PSM has been allocated
* by the application. This guarantees that all memory operations
* to this region of memory (used by multiple layers of the stack)
* always synchronize.
*/
static inline
void psmi_cuda_set_attr_sync_memops(const void *ubuf)
{
int true_flag = 1;
PSMI_CUDA_CALL(cuPointerSetAttribute, &true_flag,
CU_POINTER_ATTRIBUTE_SYNC_MEMOPS, (CUdeviceptr) ubuf);
}
#endif /* PSM_CUDA */
#define COMPILE_TIME_ASSERT(NAME,COND) extern char NAME[1/ COND]
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* _PSMI_USER_H */