2019-03-14 19:39:20 -07:00
|
|
|
/*************************************************************************
|
2021-04-12 16:00:11 -07:00
|
|
|
* Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
|
2021-04-30 16:57:36 -07:00
|
|
|
* Modifications Copyright (c) 2019-2021 Advanced Micro Devices, Inc. All rights reserved.
|
2019-03-14 19:39:20 -07:00
|
|
|
*
|
|
|
|
|
* See LICENSE.txt for license information
|
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef NCCL_ALLOC_H_
|
|
|
|
|
#define NCCL_ALLOC_H_
|
|
|
|
|
|
|
|
|
|
#include "nccl.h"
|
|
|
|
|
#include "checks.h"
|
2020-01-16 16:02:42 -08:00
|
|
|
#include "align.h"
|
2019-03-14 19:39:20 -07:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
2020-05-12 14:40:18 -07:00
|
|
|
template <typename T>
|
|
|
|
|
static ncclResult_t ncclCudaHostCalloc(T** ptr, size_t nelem) {
|
2020-06-08 20:45:19 -07:00
|
|
|
CUDACHECK(hipHostMalloc(ptr, nelem*sizeof(T), hipHostMallocMapped));
|
2020-05-12 14:40:18 -07:00
|
|
|
memset(*ptr, 0, nelem*sizeof(T));
|
2021-05-11 18:16:30 -07:00
|
|
|
INFO(NCCL_ALLOC, "Cuda Host Alloc Size %ld pointer %p", nelem*sizeof(T), *ptr);
|
2019-03-14 19:39:20 -07:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline ncclResult_t ncclCudaHostFree(void* ptr) {
|
2019-07-05 15:43:00 -07:00
|
|
|
CUDACHECK(hipHostFree(ptr));
|
2019-03-14 19:39:20 -07:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
static ncclResult_t ncclCalloc(T** ptr, size_t nelem) {
|
|
|
|
|
void* p = malloc(nelem*sizeof(T));
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
WARN("Failed to malloc %ld bytes", nelem*sizeof(T));
|
|
|
|
|
return ncclSystemError;
|
|
|
|
|
}
|
|
|
|
|
memset(p, 0, nelem*sizeof(T));
|
|
|
|
|
*ptr = (T*)p;
|
2021-05-11 18:16:30 -07:00
|
|
|
INFO(NCCL_ALLOC, "Mem Alloc Size %ld pointer %p", nelem*sizeof(T), *ptr);
|
2019-03-14 19:39:20 -07:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-06 20:32:30 -08:00
|
|
|
struct __attribute__ ((aligned(64))) allocationTracker {
|
|
|
|
|
union {
|
|
|
|
|
struct {
|
|
|
|
|
uint64_t totalAlloc;
|
|
|
|
|
uint64_t totalAllocSize;
|
|
|
|
|
};
|
|
|
|
|
char align[64];
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
static_assert(sizeof(struct allocationTracker) == 64, "allocationTracker must be size of 64 bytes");
|
|
|
|
|
#define MAX_ALLOC_TRACK_NGPU 32
|
|
|
|
|
extern struct allocationTracker allocTracker[];
|
|
|
|
|
|
2019-03-14 19:39:20 -07:00
|
|
|
template <typename T>
|
2019-07-05 15:43:00 -07:00
|
|
|
static ncclResult_t ncclCudaCalloc(T** ptr, size_t nelem, bool isFineGrain = false) {
|
2021-04-12 16:00:11 -07:00
|
|
|
// Need async stream for P2P pre-connect + CUDA Graph
|
2021-04-30 16:57:36 -07:00
|
|
|
hipStream_t stream;
|
|
|
|
|
CUDACHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
|
2019-07-05 15:43:00 -07:00
|
|
|
if (isFineGrain)
|
|
|
|
|
CUDACHECK(hipExtMallocWithFlags((void**)ptr, nelem*sizeof(T), hipDeviceMallocFinegrained));
|
|
|
|
|
else
|
|
|
|
|
CUDACHECK(hipMalloc(ptr, nelem*sizeof(T)));
|
2021-04-30 16:57:36 -07:00
|
|
|
CUDACHECK(hipMemsetAsync(*ptr, 0, nelem*sizeof(T), stream));
|
|
|
|
|
CUDACHECK(hipStreamSynchronize(stream));
|
|
|
|
|
CUDACHECK(hipStreamDestroy(stream));
|
2021-05-11 18:16:30 -07:00
|
|
|
INFO(NCCL_ALLOC, "Cuda Alloc Size %ld pointer %p", nelem*sizeof(T), *ptr);
|
2021-03-06 20:32:30 -08:00
|
|
|
int dev;
|
|
|
|
|
CUDACHECK(hipGetDevice(&dev));
|
|
|
|
|
if (dev < MAX_ALLOC_TRACK_NGPU) {
|
|
|
|
|
__atomic_fetch_add(&allocTracker[dev].totalAlloc, 1, __ATOMIC_SEQ_CST);
|
|
|
|
|
__atomic_fetch_add(&allocTracker[dev].totalAllocSize, nelem*sizeof(T), __ATOMIC_SEQ_CST);
|
|
|
|
|
}
|
2019-03-14 19:39:20 -07:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
static ncclResult_t ncclCudaMemcpy(T* dst, T* src, size_t nelem) {
|
2019-07-05 15:43:00 -07:00
|
|
|
CUDACHECK(hipMemcpy(dst, src, nelem*sizeof(T), hipMemcpyDefault));
|
2019-03-14 19:39:20 -07:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-01 15:58:49 -07:00
|
|
|
static bool hasFineGrainVramPcie() {
|
|
|
|
|
int *ptr;
|
|
|
|
|
if (hipExtMallocWithFlags((void**)&ptr, sizeof(int), hipDeviceMallocFinegrained) == hipSuccess) {
|
|
|
|
|
CUDACHECK(hipFree(ptr));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-04-01 13:21:38 -07:00
|
|
|
|
2020-01-16 16:02:42 -08:00
|
|
|
// Allocate memory to be potentially ibv_reg_mr'd. This needs to be
|
|
|
|
|
// allocated on separate pages as those pages will be marked DONTFORK
|
|
|
|
|
// and if they are shared, that could cause a crash in a child process
|
|
|
|
|
static ncclResult_t ncclIbMalloc(void** ptr, size_t size) {
|
|
|
|
|
size_t page_size = sysconf(_SC_PAGESIZE);
|
|
|
|
|
void* p;
|
|
|
|
|
int size_aligned = ROUNDUP(size, page_size);
|
|
|
|
|
int ret = posix_memalign(&p, page_size, size_aligned);
|
|
|
|
|
if (ret != 0) return ncclSystemError;
|
|
|
|
|
memset(p, 0, size);
|
|
|
|
|
*ptr = p;
|
2021-05-11 18:16:30 -07:00
|
|
|
INFO(NCCL_ALLOC, "Ib Alloc Size %ld pointer %p", size, *ptr);
|
2020-01-16 16:02:42 -08:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-14 19:39:20 -07:00
|
|
|
#endif
|