forked from cornelisnetworks/opa-psm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpsm_user.h
481 lines (405 loc) · 15.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
/*
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) 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:
Intel Corporation, www.intel.com
BSD LICENSE
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
#include "psm_config.h"
#include <inttypes.h>
#include <pthread.h>
#include <sched.h>
#include <numa.h>
#include <semaphore.h>
#include <fcntl.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 cuda_lib_version;
extern CUcontext ctxt;
void *psmi_cuda_lib;
CUresult (*psmi_cuInit)(unsigned int Flags );
CUresult (*psmi_cuCtxDetach)(CUcontext c);
CUresult (*psmi_cuCtxSetCurrent)(CUcontext c);
CUresult (*psmi_cuCtxGetCurrent)(CUcontext *c);
CUresult (*psmi_cuCtxSetCurrent)(CUcontext c);
CUresult (*psmi_cuPointerGetAttribute)(void *data, CUpointer_attribute pa, CUdeviceptr p);
CUresult (*psmi_cuPointerSetAttribute)(void *data, CUpointer_attribute pa, CUdeviceptr p);
CUresult (*psmi_cuDeviceGet)(CUdevice* device, int ordinal);
CUresult (*psmi_cuDeviceGetAttribute)(int* pi, CUdevice_attribute attrib, CUdevice dev);
CUresult (*psmi_cuDriverGetVersion)(int* driverVersion);
CUresult (*psmi_cuDeviceGetCount)(int* count);
CUresult (*psmi_cuStreamCreate)(CUstream* phStream, unsigned int Flags);
CUresult (*psmi_cuStreamDestroy)(CUstream phStream);
CUresult (*psmi_cuEventCreate)(CUevent* phEvent, unsigned int Flags);
CUresult (*psmi_cuEventDestroy)(CUevent hEvent);
CUresult (*psmi_cuEventQuery)(CUevent hEvent);
CUresult (*psmi_cuEventRecord)(CUevent hEvent, CUstream hStream);
CUresult (*psmi_cuEventSynchronize)(CUevent hEvent);
CUresult (*psmi_cuMemHostAlloc)(void** pp, size_t bytesize, unsigned int Flags);
CUresult (*psmi_cuMemFreeHost)(void* p);
CUresult (*psmi_cuMemcpy)(CUdeviceptr dst, CUdeviceptr src, size_t ByteCount);
CUresult (*psmi_cuMemcpyDtoD)(CUdeviceptr dstDevice, CUdeviceptr srcDevice, size_t ByteCount);
CUresult (*psmi_cuMemcpyDtoH)(void* dstHost, CUdeviceptr srcDevice, size_t ByteCount);
CUresult (*psmi_cuMemcpyHtoD)(CUdeviceptr dstDevice, const void* srcHost, size_t ByteCount);
CUresult (*psmi_cuMemcpyDtoHAsync)(void* dstHost, CUdeviceptr srcDevice, size_t ByteCount, CUstream hStream);
CUresult (*psmi_cuMemcpyHtoDAsync)(CUdeviceptr dstDevice, const void* srcHost, size_t ByteCount, CUstream hStream);
CUresult (*psmi_cuIpcGetMemHandle)(CUipcMemHandle* pHandle, CUdeviceptr dptr);
CUresult (*psmi_cuIpcOpenMemHandle)(CUdeviceptr* pdptr, CUipcMemHandle handle, unsigned int Flags);
CUresult (*psmi_cuIpcCloseMemHandle)(CUdeviceptr dptr);
CUresult (*psmi_cuMemGetAddressRange)(CUdeviceptr* pbase, size_t* psize, CUdeviceptr dptr);
CUresult (*psmi_cuDevicePrimaryCtxGetState)(CUdevice dev, unsigned int* flags, int* active);
CUresult (*psmi_cuDevicePrimaryCtxRetain)(CUcontext* pctx, CUdevice dev);
CUresult (*psmi_cuCtxGetDevice)(CUdevice* device);
CUresult (*psmi_cuDevicePrimaryCtxRelease)(CUdevice device);
#define PSMI_CUDA_CALL(func, args...) do { \
CUresult cudaerr; \
cudaerr = psmi_##func(args); \
if (cudaerr != CUDA_SUCCESS) { \
if (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");\
} \
} while (0)
#define PSMI_CUDA_CHECK_EVENT(event, cudaerr) do { \
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(void *ptr))
{
CUresult cres;
CUmemorytype mt;
cres = psmi_cuPointerGetAttribute(
&mt, CU_POINTER_ATTRIBUTE_MEMORY_TYPE, (CUdeviceptr) ptr);
if ((cres == CUDA_SUCCESS) && (mt == CU_MEMORYTYPE_DEVICE))
return 1;
else
return 0;
}
PSMI_ALWAYS_INLINE(
int
_psmi_is_cuda_enabled())
{
return is_cuda_enabled;
}
#define PSMI_IS_CUDA_ENABLED _psmi_is_cuda_enabled()
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 *context, 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 && \
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;
#endif /* PSM_CUDA */
#define COMPILE_TIME_ASSERT(NAME,COND) extern char NAME[1/ COND]
#endif /* _PSMI_USER_H */