From 32777231730983aa38bb92624ca0b30fd75fb521 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Sun, 6 Jan 2019 18:54:25 -0800 Subject: [PATCH] Replace some malloc+memset pairs with calloc. Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/15765 Differential Revision: D13588723 Pulled By: resistor fbshipit-source-id: 47d35dc608847a5b173cfcf2aaa2a77359e56722 --- aten/src/THC/THCGeneral.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/aten/src/THC/THCGeneral.cpp b/aten/src/THC/THCGeneral.cpp index 0290e5e..b18a6da 100644 --- a/aten/src/THC/THCGeneral.cpp +++ b/aten/src/THC/THCGeneral.cpp @@ -33,8 +33,7 @@ THCCudaResourcesPerDevice* THCState_getDeviceResourcePtr( THCState* THCState_alloc(void) { - THCState* state = (THCState*) malloc(sizeof(THCState)); - memset(state, 0, sizeof(THCState)); + THCState* state = (THCState*) calloc(1, sizeof(THCState)); return state; } @@ -55,8 +54,7 @@ void THCudaInit(THCState* state) THCudaCheck(cudaGetDevice(&device)); state->resourcesPerDevice = (THCCudaResourcesPerDevice*) - malloc(numDevices * sizeof(THCCudaResourcesPerDevice)); - memset(state->resourcesPerDevice, 0, numDevices * sizeof(THCCudaResourcesPerDevice)); + calloc(numDevices, sizeof(THCCudaResourcesPerDevice)); state->deviceProperties = (struct cudaDeviceProp*)malloc(numDevices * sizeof(struct cudaDeviceProp)); @@ -69,14 +67,12 @@ void THCudaInit(THCState* state) // "-1" (unknown). // Currently the max number of gpus in P2P group is 8, so if there are more // we enable P2P in groups of 8 - state->p2pAccessEnabled = (int**) malloc(sizeof(int*) * numDevices); + state->p2pAccessEnabled = (int**) calloc(numDevices, sizeof(int*)); for (int i = 0; i < numDevices; ++i) { - state->p2pAccessEnabled[i] = (int*) malloc(sizeof(int) * numDevices); + state->p2pAccessEnabled[i] = (int*) calloc(numDevices, sizeof(int)); for (int j = 0; j < numDevices; ++j) if (i == j) state->p2pAccessEnabled[i][j] = 1; - else if (j / THC_CUDA_MAX_PEER_SIZE != i / THC_CUDA_MAX_PEER_SIZE) - state->p2pAccessEnabled[i][j] = 0; else state->p2pAccessEnabled[i][j] = -1; } -- 2.7.4