Replace some malloc+memset pairs with calloc.
authorOwen Anderson <owen.anderson@oculus.com>
Mon, 7 Jan 2019 02:54:25 +0000 (18:54 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 7 Jan 2019 02:57:17 +0000 (18:57 -0800)
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

index 0290e5e..b18a6da 100644 (file)
@@ -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;
   }