Skip to content

Commit 5c126af

Browse files
committed
Revert "Auto device: API changes, bug fixes, README.md"
This reverts commit d88ac24c712e3a40d4aaf3ac2d043bd79ba4280e. Revert "Auto device mode, plus allocation helper functions." This reverts commit 47a2f6de252c2254234edfc1c6115229b5383bac.
1 parent 3ef5ad2 commit 5c126af

File tree

8 files changed

+14
-114
lines changed

8 files changed

+14
-114
lines changed

THCGeneral.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ void THCudaInit(THCState* state)
2121
state->deviceProperties =
2222
(struct cudaDeviceProp*)malloc(count * sizeof(struct cudaDeviceProp));
2323

24-
THCState_setDeviceMode(state, THCStateDeviceModeManual);
25-
2624
state->numUserStreams = 0;
2725
state->streamsPerDevice =
2826
(cudaStream_t**)malloc(count * sizeof(cudaStream_t*));
@@ -117,30 +115,6 @@ int THCState_getNumDevices(THCState *state)
117115
return state->numDevices;
118116
}
119117

120-
THCStateDeviceMode THCState_getDeviceMode(THCState* state)
121-
{
122-
return state->deviceMode;
123-
}
124-
125-
void THCState_setDeviceMode(THCState* state, THCStateDeviceMode mode)
126-
{
127-
state->deviceMode = mode;
128-
}
129-
130-
void THCState_setDevice(THCState *state, int device)
131-
{
132-
int curDev;
133-
THCudaCheck(cudaGetDevice(&curDev));
134-
if (device != curDev) {
135-
THCudaCheck(cudaSetDevice(device));
136-
THCRandom_setGenerator(state, device);
137-
THCudaBlas_setHandle(state, device);
138-
139-
/* The stream is per device, so update the stream as well */
140-
THCState_setStream(state, device, THCState_getCurrentStreamIndex(state));
141-
}
142-
}
143-
144118
void THCState_reserveStreams(THCState* state, int numStreams)
145119
{
146120
if (numStreams <= state->numUserStreams)

THCGeneral.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
struct THCRNGState; /* Random number generator state. */
3737
struct THCBlasState;
3838

39-
typedef enum THCStateDeviceMode {
40-
THCStateDeviceModeManual,
41-
THCStateDeviceModeAuto
42-
} THCStateDeviceMode;
43-
4439
/* Global state to be held in the cutorch table. */
4540
typedef struct THCState
4641
{
@@ -60,8 +55,6 @@ typedef struct THCState
6055
/* Index of the current selected per-device stream. Actual CUDA stream changes
6156
based on the current device, since streams are per-device */
6257
int currentPerDeviceStream;
63-
/* in DeviceModeAuto, cutorch can set the device based on the location of data tensors */
64-
THCStateDeviceMode deviceMode;
6558
} THCState;
6659

6760
THC_API void THCudaBlas_init(THCState *state, int num_devices, int current_device);
@@ -75,9 +68,6 @@ THC_API void THCudaEnablePeerToPeerAccess(THCState* state);
7568

7669
/* State manipulators and accessors */
7770
THC_API int THCState_getNumDevices(THCState* state);
78-
THC_API void THCState_setDevice(THCState* state, int device);
79-
THC_API THCStateDeviceMode THCState_getDeviceMode(THCState* state);
80-
THC_API void THCState_setDeviceMode(THCState* state, THCStateDeviceMode mode);
8171
THC_API void THCState_reserveStreams(THCState* state, int numStreams);
8272
THC_API int THCState_getNumStreams(THCState* state);
8373

THCStorage.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ THCudaStorage* THCudaStorage_new(THCState *state)
2222
storage->data = NULL;
2323
storage->size = 0;
2424
storage->refcount = 1;
25-
storage->device = THC_DEVICE_NONE;
2625
storage->flag = TH_STORAGE_REFCOUNTED | TH_STORAGE_RESIZABLE | TH_STORAGE_FREEMEM;
2726
return storage;
2827
}
@@ -38,7 +37,6 @@ THCudaStorage* THCudaStorage_newWithSize(THCState *state, long size)
3837

3938
storage->size = size;
4039
storage->refcount = 1;
41-
THCudaCheck(cudaGetDevice(&storage->device));
4240
storage->flag = TH_STORAGE_REFCOUNTED | TH_STORAGE_RESIZABLE | TH_STORAGE_FREEMEM;
4341
return storage;
4442
}
@@ -94,11 +92,6 @@ THCudaStorage* THCudaStorage_newWithData(THCState *state, float *data, long size
9492
storage->data = data;
9593
storage->size = size;
9694
storage->refcount = 1;
97-
if(size == 0) {
98-
storage->device = THC_DEVICE_NONE;
99-
} else {
100-
THCudaCheck(cudaGetDevice(&storage->device));
101-
}
10295
storage->flag = TH_STORAGE_REFCOUNTED | TH_STORAGE_RESIZABLE | TH_STORAGE_FREEMEM;
10396
return storage;
10497
}

THCStorage.cu

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,6 @@ void THCudaStorage_resize(THCState *state, THCudaStorage *self, long size)
2626
}
2727
else
2828
{
29-
int curDev;
30-
THCudaCheck(cudaGetDevice(&curDev));
31-
if(self->device != THC_DEVICE_NONE) {
32-
if (THCState_getDeviceMode(state) == THCStateDeviceModeAuto) {
33-
THCudaCheck(cudaSetDevice(self->device));
34-
}
35-
else if(self->device != curDev) {
36-
THError("THCudaStorage_resize: device mismatch: tensorDev=%d, curDev=%d", self->device + 1, curDev + 1);
37-
}
38-
}
39-
4029
float *data = NULL;
4130
THCudaCheck(cudaMalloc((void**)(&data), size * sizeof(float)));
4231

@@ -51,19 +40,5 @@ void THCudaStorage_resize(THCState *state, THCudaStorage *self, long size)
5140

5241
self->data = data;
5342
self->size = size;
54-
THCudaCheck(cudaGetDevice(&self->device));
55-
56-
THCudaCheck(cudaSetDevice(curDev));
5743
}
5844
}
59-
60-
int THCudaStorage_getDevice(THCState* state, const THCudaStorage *storage) {
61-
return storage->device;
62-
}
63-
64-
void THCudaStorage_setDevice(THCState* state, THCudaStorage *storage, int device) {
65-
if(storage->size > 0 && storage->device != device) {
66-
THError("Cannot call setDevice() on a non-empty tensor. Use copy() instead.");
67-
}
68-
storage->device = device;
69-
}

THCStorage.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
#define TH_STORAGE_RESIZABLE 2
99
#define TH_STORAGE_FREEMEM 4
1010

11-
#define THC_DEVICE_NONE -1
1211

1312
typedef struct THCudaStorage
1413
{
1514
float *data;
1615
long size;
1716
int refcount;
18-
int device;
1917
char flag;
2018
THAllocator *allocator;
2119
void *allocatorContext;
@@ -54,7 +52,4 @@ THC_API void THCudaStorage_free(THCState *state, THCudaStorage *storage);
5452
THC_API void THCudaStorage_resize(THCState *state, THCudaStorage *storage, long size);
5553
THC_API void THCudaStorage_fill(THCState *state, THCudaStorage *storage, float value);
5654

57-
THC_API int THCudaStorage_getDevice(THCState* state, const THCudaStorage *storage);
58-
THC_API void THCudaStorage_setDevice(THCState* state, THCudaStorage *storage, int device);
59-
6055
#endif

THCTensor.c

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void THCudaTensor_freeCopyTo(THCState *state, THCudaTensor *self, THCudaTensor *
600600
static void THCudaTensor_rawInit(THCState *state, THCudaTensor *self)
601601
{
602602
self->refcount = 1;
603-
self->storage = THCudaStorage_new(state);
603+
self->storage = NULL;
604604
self->storageOffset = 0;
605605
self->size = NULL;
606606
self->stride = NULL;
@@ -610,7 +610,6 @@ static void THCudaTensor_rawInit(THCState *state, THCudaTensor *self)
610610

611611
static void THCudaTensor_rawSet(THCState *state, THCudaTensor *self, THCudaStorage *storage, long storageOffset, int nDimension, long *size, long *stride)
612612
{
613-
THAssert(self->storage != NULL);
614613
/* storage */
615614
if(self->storage != storage)
616615
{
@@ -623,7 +622,7 @@ static void THCudaTensor_rawSet(THCState *state, THCudaTensor *self, THCudaStora
623622
THCudaStorage_retain(state, self->storage);
624623
}
625624
else
626-
self->storage = THCudaStorage_new(state);
625+
self->storage = NULL;
627626
}
628627

629628
/* storageOffset */
@@ -760,39 +759,19 @@ float THCudaTensor_get4d(THCState *state, const THCudaTensor *tensor, long x0, l
760759

761760
int THCudaTensor_checkGPU(THCState *state, unsigned int nTensors, ...)
762761
{
763-
int kernelDev;
764-
if (THCState_getDeviceMode(state) == THCStateDeviceModeManual) {
765-
THCudaCheck(cudaGetDevice(&kernelDev));
766-
} else {
767-
kernelDev = THC_DEVICE_NONE;
768-
}
769-
762+
int curDev = -1;
763+
THCudaCheck(cudaGetDevice(&curDev));
770764
va_list(args);
771765
va_start(args, nTensors);
766+
int valid = 1;
772767
for (unsigned int i = 0; i < nTensors; i++) {
773768
THCudaTensor* tensor = va_arg(args, THCudaTensor*);
774-
if(tensor == NULL) {
775-
continue;
776-
}
777769
int tensorDev = THCudaTensor_getDevice(state, tensor);
778-
if (tensorDev != THC_DEVICE_NONE) {
779-
if (kernelDev != tensorDev && kernelDev != THC_DEVICE_NONE) {
780-
va_end(args);
781-
return 0; // device mismatch
782-
} else {
783-
kernelDev = tensorDev;
784-
}
770+
if (tensorDev != -1 && tensorDev != curDev) {
771+
valid = 0;
772+
break;
785773
}
786774
}
787775
va_end(args);
788-
789-
if (THCState_getDeviceMode(state) == THCStateDeviceModeAuto) {
790-
if (kernelDev == THC_DEVICE_NONE) {
791-
return 0; // cannot determine device
792-
} else {
793-
THCState_setDevice(state, kernelDev);
794-
}
795-
}
796-
797-
return 1;
776+
return valid;
798777
}

THCTensor.cu

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ cudaTextureObject_t THCudaTensor_getTextureObject(THCState *state, THCudaTensor
2626
return texObj;
2727
}
2828

29-
int THCudaTensor_getDevice(THCState* state, const THCudaTensor* self) {
30-
THCudaStorage *storage = THCudaTensor_storage(state, self);
31-
THAssert(storage != NULL);
32-
return THCudaStorage_getDevice(state, storage);
29+
THC_API int THCudaTensor_getDevice(THCState* state, const THCudaTensor* thc) {
30+
if (!thc->storage) return -1;
31+
cudaPointerAttributes attr;
32+
THCudaCheck(cudaPointerGetAttributes(&attr, thc->storage->data));
33+
return attr.device;
3334
}
34-
35-
void THCudaTensor_setDevice(THCState* state, THCudaTensor* self, int device) {
36-
THCudaStorage *storage = THCudaTensor_storage(state, self);
37-
THAssert(storage != NULL);
38-
THCudaStorage_setDevice(state, storage, device);
39-
}

THCTensor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ THC_API float THCudaTensor_get4d(THCState *state, const THCudaTensor *tensor, lo
125125
/* CUDA-specific functions */
126126
THC_API cudaTextureObject_t THCudaTensor_getTextureObject(THCState *state, THCudaTensor *self);
127127
THC_API int THCudaTensor_getDevice(THCState *state, const THCudaTensor *self);
128-
THC_API void THCudaTensor_setDevice(THCState* state, THCudaTensor* self, int device);
129128
THC_API int THCudaTensor_checkGPU(THCState *state, unsigned int nTensors, ...);
130129

131130
#endif

0 commit comments

Comments
 (0)