/* (1) if the torch-allocated heap size exceeds the soft max, run GC * (2) if post-GC heap size exceeds 80% of the soft max, increase the * soft max by 40% */ static void maybeTriggerGC(ptrdiff_t curHeapSize) { if (torchGCFunction && curHeapSize > heapSoftmax) { torchGCFunction(torchGCData); // ensure heapSize is accurate before updating heapSoftmax ptrdiff_t newHeapSize = applyHeapDelta(); if (newHeapSize > heapSoftmax * heapSoftmaxGrowthThresh) { heapSoftmax = (ptrdiff_t)(heapSoftmax * heapSoftmaxGrowthFactor); } } }
void THCHeapUpdate(THCState *state, long size) { state->heapDelta += size; // batch updates to global heapSize to minimize thread contention if (abs(state->heapDelta) < heapMaxDelta) { return; } long newHeapSize = applyHeapDelta(state); if (size > 0) { maybeTriggerGC(state, newHeapSize); } }
// Here we maintain a dynamic softmax threshold for THC-allocated storages. // When THC heap size goes above this softmax, the GC hook is triggered. // If heap size is above 80% of the softmax after GC, then the softmax is // increased. static void maybeTriggerGC(THCState *state, long curHeapSize) { if (state->cutorchGCFunction != NULL && curHeapSize > state->heapSoftmax) { (state->cutorchGCFunction)(state->cutorchGCData); // ensure heapSize is accurate before updating heapSoftmax long newHeapSize = applyHeapDelta(state); if (newHeapSize > state->heapSoftmax * heapSoftmaxGrowthThresh) { state->heapSoftmax = state->heapSoftmax * heapSoftmaxGrowthFactor; } } }
void THCHeapUpdate(THCState *state, ptrdiff_t size) { state->heapDelta += size; // batch updates to global heapSize to minimize thread contention if (state->heapDelta < heapMaxDelta && state->heapDelta > heapMinDelta) { return; } ptrdiff_t newHeapSize = applyHeapDelta(state); if (size > 0) { maybeTriggerGC(state, newHeapSize); } }
// hooks into the TH heap tracking void THHeapUpdate(long size) { heapDelta += size; // batch updates to global heapSize to minimize thread contention if (labs(heapDelta) < heapMaxDelta) { return; } long newHeapSize = applyHeapDelta(); if (size > 0) { maybeTriggerGC(newHeapSize); } }
// hooks into the TH heap tracking void THHeapUpdate(ptrdiff_t size) { #ifdef DEBUG if (size > 0 && heapDelta > PTRDIFF_MAX - size) THError("THHeapUpdate: heapDelta(%td) + increased(%td) > PTRDIFF_MAX, heapDelta overflow!", heapDelta, size); if (size < 0 && heapDelta < PTRDIFF_MIN - size) THError("THHeapUpdate: heapDelta(%td) + decreased(%td) < PTRDIFF_MIN, heapDelta underflow!", heapDelta, size); #endif heapDelta += size; // batch updates to global heapSize to minimize thread contention if (heapDelta < heapMaxDelta && heapDelta > heapMinDelta) { return; } ptrdiff_t newHeapSize = applyHeapDelta(); if (size > 0) { maybeTriggerGC(newHeapSize); } }