2019-03-14 19:39:20 -07:00
|
|
|
/*************************************************************************
|
2022-01-07 06:39:55 -08:00
|
|
|
* Copyright (c) 2019-2022, NVIDIA CORPORATION. All rights reserved.
|
2022-04-18 11:14:51 -07:00
|
|
|
* Modifications Copyright (c) 2019-2022 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"
|
2022-05-24 02:02:31 -07:00
|
|
|
#include "utils.h"
|
2019-03-14 19:39:20 -07:00
|
|
|
#include <sys/mman.h>
|
2021-09-08 13:56:25 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2022-09-06 10:29:46 -06:00
|
|
|
#include "rccl_vars.h"
|
2019-03-14 19:39:20 -07:00
|
|
|
|
2022-05-24 02:02:31 -07:00
|
|
|
uint64_t clockNano(); // from utils.h with which we have a circular dependency
|
|
|
|
|
|
2020-05-12 14:40:18 -07:00
|
|
|
template <typename T>
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t ncclCudaHostCallocDebug(T** ptr, size_t nelem, const char *filefunc, int line) {
|
|
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
2022-05-24 02:02:31 -07:00
|
|
|
*ptr = nullptr;
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
|
|
|
|
CUDACHECKGOTO(hipHostMalloc(ptr, nelem*sizeof(T), hipHostMallocMapped), result, finish);
|
2020-05-12 14:40:18 -07:00
|
|
|
memset(*ptr, 0, nelem*sizeof(T));
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-08-18 02:53:17 -07:00
|
|
|
if (*ptr == nullptr) WARN("Failed to CUDA host alloc %ld bytes", nelem*sizeof(T));
|
|
|
|
|
INFO(NCCL_ALLOC, "%s:%d Cuda Host Alloc Size %ld pointer %p", filefunc, line, nelem*sizeof(T), *ptr);
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
2019-03-14 19:39:20 -07:00
|
|
|
}
|
2021-07-08 14:12:04 -07:00
|
|
|
#define ncclCudaHostCalloc(...) ncclCudaHostCallocDebug(__VA_ARGS__, __FILE__, __LINE__)
|
2019-03-14 19:39:20 -07:00
|
|
|
|
2022-05-24 02:02:31 -07:00
|
|
|
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>
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t ncclCallocDebug(T** ptr, size_t nelem, const char *filefunc, int line) {
|
2019-03-14 19:39:20 -07:00
|
|
|
void* p = malloc(nelem*sizeof(T));
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
WARN("Failed to malloc %ld bytes", nelem*sizeof(T));
|
|
|
|
|
return ncclSystemError;
|
|
|
|
|
}
|
2022-01-07 06:39:55 -08:00
|
|
|
//INFO(NCCL_ALLOC, "%s:%d malloc Size %ld pointer %p", filefunc, line, nelem*sizeof(T), p);
|
2019-03-14 19:39:20 -07:00
|
|
|
memset(p, 0, nelem*sizeof(T));
|
|
|
|
|
*ptr = (T*)p;
|
|
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
2022-01-07 06:39:55 -08:00
|
|
|
#define ncclCalloc(...) ncclCallocDebug(__VA_ARGS__, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t ncclRealloc(T** ptr, size_t oldNelem, size_t nelem) {
|
2022-01-07 06:39:55 -08:00
|
|
|
if (nelem < oldNelem) return ncclInternalError;
|
|
|
|
|
if (nelem == oldNelem) return ncclSuccess;
|
|
|
|
|
|
|
|
|
|
T* oldp = *ptr;
|
|
|
|
|
T* p = (T*)malloc(nelem*sizeof(T));
|
|
|
|
|
if (p == NULL) {
|
|
|
|
|
WARN("Failed to malloc %ld bytes", nelem*sizeof(T));
|
|
|
|
|
return ncclSystemError;
|
|
|
|
|
}
|
|
|
|
|
memcpy(p, oldp, oldNelem*sizeof(T));
|
|
|
|
|
free(oldp);
|
|
|
|
|
memset(p+oldNelem, 0, (nelem-oldNelem)*sizeof(T));
|
|
|
|
|
*ptr = (T*)p;
|
|
|
|
|
INFO(NCCL_ALLOC, "Mem Realloc old size %ld, new size %ld pointer %p", oldNelem*sizeof(T), nelem*sizeof(T), *ptr);
|
|
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
2019-03-14 19:39:20 -07:00
|
|
|
|
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>
|
2022-09-09 01:20:52 +00:00
|
|
|
ncclResult_t ncclCudaMallocDebug(const char *filefunc, int line, T** ptr, size_t nelem, bool isFineGrain = false) {
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
2022-05-24 02:02:31 -07:00
|
|
|
*ptr = nullptr;
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
|
|
|
|
if (isFineGrain)
|
|
|
|
|
CUDACHECKGOTO(hipExtMallocWithFlags((void**)ptr, nelem*sizeof(T), hipDeviceMallocFinegrained), result, finish);
|
|
|
|
|
else
|
|
|
|
|
CUDACHECKGOTO(hipMalloc(ptr, nelem*sizeof(T)), result, finish);
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-08-18 02:53:17 -07:00
|
|
|
if (*ptr == nullptr) WARN("Failed to CUDA malloc %ld bytes", nelem*sizeof(T));
|
|
|
|
|
INFO(NCCL_ALLOC, "%s:%d Cuda Alloc Size %ld pointer %p", filefunc, line, nelem*sizeof(T), *ptr);
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
2022-09-09 01:20:52 +00:00
|
|
|
#define ncclCudaMalloc(...) ncclCudaMallocDebug( __FILE__, __LINE__, __VA_ARGS__)
|
2022-09-06 10:29:46 -06:00
|
|
|
|
2022-05-24 02:02:31 -07:00
|
|
|
template <typename T>
|
2022-09-09 23:07:04 +00:00
|
|
|
ncclResult_t ncclCudaCallocDebug(const char *filefunc, int line, T** ptr, size_t nelem, hipStream_t sideStream = nullptr, bool isFineGrain = false) {
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
2022-05-24 02:02:31 -07:00
|
|
|
*ptr = nullptr;
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-05-24 02:02:31 -07:00
|
|
|
// Need a side stream so as not to interfere with graph capture.
|
2022-09-09 23:07:04 +00:00
|
|
|
hipStream_t stream = sideStream;
|
|
|
|
|
if (stream == nullptr)
|
|
|
|
|
CUDACHECK(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking));
|
2019-07-05 15:43:00 -07:00
|
|
|
if (isFineGrain)
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECKGOTO(hipExtMallocWithFlags((void**)ptr, nelem*sizeof(T), hipDeviceMallocFinegrained), result, finish);
|
2019-07-05 15:43:00 -07:00
|
|
|
else
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECKGOTO(hipMalloc(ptr, nelem*sizeof(T)), result, finish);
|
|
|
|
|
CUDACHECKGOTO(hipMemsetAsync(*ptr, 0, nelem*sizeof(T), stream), result, finish);
|
|
|
|
|
CUDACHECKGOTO(hipStreamSynchronize(stream), result, finish);
|
2022-09-09 23:07:04 +00:00
|
|
|
if (sideStream == nullptr)
|
|
|
|
|
CUDACHECKGOTO(hipStreamDestroy(stream), result, finish);
|
2022-09-09 01:20:52 +00:00
|
|
|
int dev;
|
|
|
|
|
CUDACHECK(hipGetDevice(&dev));
|
|
|
|
|
if (dev < MAX_ALLOC_TRACK_NGPU) {
|
|
|
|
|
__atomic_fetch_add(&allocTracker[dev].totalAlloc, 1, __ATOMIC_RELAXED);
|
|
|
|
|
__atomic_fetch_add(&allocTracker[dev].totalAllocSize, nelem*sizeof(T), __ATOMIC_RELAXED);
|
2022-09-06 10:29:46 -06:00
|
|
|
}
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-08-18 02:53:17 -07:00
|
|
|
if (*ptr == nullptr) WARN("Failed to CUDA calloc %ld bytes", nelem*sizeof(T));
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-08-18 02:53:17 -07:00
|
|
|
INFO(NCCL_ALLOC, "%s:%d Cuda Alloc Size %ld pointer %p", filefunc, line, nelem*sizeof(T), *ptr);
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
2019-03-14 19:39:20 -07:00
|
|
|
}
|
2022-09-09 01:20:52 +00:00
|
|
|
#define ncclCudaCalloc(...) ncclCudaCallocDebug(__FILE__, __LINE__, __VA_ARGS__)
|
2022-09-06 10:29:46 -06:00
|
|
|
|
2019-03-14 19:39:20 -07:00
|
|
|
template <typename T>
|
2022-09-09 01:20:52 +00:00
|
|
|
ncclResult_t ncclCudaCallocAsyncDebug(const char *filefunc, int line, T** ptr, size_t nelem, hipStream_t stream, bool isFineGrain = false) {
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
2022-05-24 02:02:31 -07:00
|
|
|
*ptr = nullptr;
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
|
|
|
|
if (isFineGrain)
|
|
|
|
|
CUDACHECKGOTO(hipExtMallocWithFlags((void**)ptr, nelem*sizeof(T), hipDeviceMallocFinegrained), result, finish);
|
|
|
|
|
else
|
|
|
|
|
CUDACHECKGOTO(hipMalloc(ptr, nelem*sizeof(T)), result, finish);
|
|
|
|
|
CUDACHECKGOTO(hipMemsetAsync(*ptr, 0, nelem*sizeof(T), stream), result, finish);
|
2021-03-06 20:32:30 -08:00
|
|
|
int dev;
|
|
|
|
|
CUDACHECK(hipGetDevice(&dev));
|
|
|
|
|
if (dev < MAX_ALLOC_TRACK_NGPU) {
|
2022-09-09 01:20:52 +00:00
|
|
|
__atomic_fetch_add(&allocTracker[dev].totalAlloc, 1, __ATOMIC_RELAXED);
|
|
|
|
|
__atomic_fetch_add(&allocTracker[dev].totalAllocSize, nelem*sizeof(T), __ATOMIC_RELAXED);
|
2021-03-06 20:32:30 -08:00
|
|
|
}
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-08-18 02:53:17 -07:00
|
|
|
if (*ptr == nullptr) WARN("Failed to CUDA calloc async %ld bytes", nelem*sizeof(T));
|
|
|
|
|
INFO(NCCL_ALLOC, "%s:%d Cuda Alloc Size %ld pointer %p", filefunc, line, nelem*sizeof(T), *ptr);
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
2019-03-14 19:39:20 -07:00
|
|
|
}
|
2022-09-09 01:20:52 +00:00
|
|
|
#define ncclCudaCallocAsync(...) ncclCudaCallocAsyncDebug(__FILE__, __LINE__, __VA_ARGS__)
|
2019-03-14 19:39:20 -07:00
|
|
|
|
|
|
|
|
template <typename T>
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t ncclCudaMemcpy(T* dst, T* src, size_t nelem) {
|
|
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
|
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-05-24 02:02:31 -07:00
|
|
|
// Need a side stream so as not to interfere with graph capture.
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStream_t stream;
|
|
|
|
|
CUDACHECKGOTO(hipStreamCreateWithFlags(&stream, hipStreamNonBlocking), result, finish);
|
2022-05-24 02:02:31 -07:00
|
|
|
NCCLCHECKGOTO(ncclCudaMemcpyAsync(dst, src, nelem, stream), result, finish);
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECKGOTO(hipStreamSynchronize(stream), result, finish);
|
|
|
|
|
CUDACHECKGOTO(hipStreamDestroy(stream), result, finish);
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2022-09-09 01:20:52 +00:00
|
|
|
ncclResult_t ncclCudaMemcpyAsync(T* dst, T* src, size_t nelem, hipStream_t stream) {
|
2022-05-24 02:02:31 -07:00
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
|
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
|
|
|
|
CUDACHECKGOTO(hipMemcpyAsync(dst, src, nelem*sizeof(T), hipMemcpyDefault, stream), result, finish);
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
ncclResult_t ncclCudaFree(T* ptr) {
|
|
|
|
|
ncclResult_t result = ncclSuccess;
|
2022-09-09 01:20:52 +00:00
|
|
|
hipStreamCaptureMode mode = hipStreamCaptureModeRelaxed;
|
|
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
|
|
|
|
CUDACHECKGOTO(hipFree(ptr), result, finish);
|
2022-05-24 02:02:31 -07:00
|
|
|
finish:
|
2022-09-09 01:20:52 +00:00
|
|
|
CUDACHECK(hipThreadExchangeStreamCaptureMode(&mode));
|
2022-05-24 02:02:31 -07:00
|
|
|
return result;
|
2019-03-14 19:39:20 -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
|
2022-05-24 02:02:31 -07:00
|
|
|
inline ncclResult_t ncclIbMallocDebug(void** ptr, size_t size, const char *filefunc, int line) {
|
2020-01-16 16:02:42 -08:00
|
|
|
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-07-08 14:12:04 -07:00
|
|
|
INFO(NCCL_ALLOC, "%s:%d Ib Alloc Size %ld pointer %p", filefunc, line, size, *ptr);
|
2020-01-16 16:02:42 -08:00
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
2021-07-08 14:12:04 -07:00
|
|
|
#define ncclIbMalloc(...) ncclIbMallocDebug(__VA_ARGS__, __FILE__, __LINE__)
|
2020-01-16 16:02:42 -08:00
|
|
|
|
2019-03-14 19:39:20 -07:00
|
|
|
#endif
|