2019-03-14 19:39:20 -07:00
|
|
|
/*************************************************************************
|
|
|
|
|
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* See LICENSE.txt for license information
|
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
|
|
#ifndef NCCL_ALLOC_H_
|
|
|
|
|
#define NCCL_ALLOC_H_
|
|
|
|
|
|
|
|
|
|
#include "nccl.h"
|
|
|
|
|
#include "checks.h"
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
|
|
static inline ncclResult_t ncclCudaHostAlloc(void** ptr, void** devPtr, size_t size) {
|
2019-07-05 15:43:00 -07:00
|
|
|
CUDACHECK(hipHostMalloc(ptr, size, hipHostMallocMapped));
|
2019-03-14 19:39:20 -07:00
|
|
|
memset(*ptr, 0, size);
|
|
|
|
|
*devPtr = *ptr;
|
|
|
|
|
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;
|
|
|
|
|
return ncclSuccess;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2019-07-05 15:43:00 -07:00
|
|
|
static ncclResult_t ncclCudaCalloc(T** ptr, size_t nelem, bool isFineGrain = false) {
|
|
|
|
|
if (isFineGrain)
|
|
|
|
|
CUDACHECK(hipExtMallocWithFlags((void**)ptr, nelem*sizeof(T), hipDeviceMallocFinegrained));
|
|
|
|
|
else
|
|
|
|
|
CUDACHECK(hipMalloc(ptr, nelem*sizeof(T)));
|
|
|
|
|
CUDACHECK(hipMemset(*ptr, 0, nelem*sizeof(T)));
|
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;
|
|
|
|
|
}
|
2019-03-14 19:39:20 -07:00
|
|
|
#endif
|