Skip to content

Commit 93f134b

Browse files
authored
mem leaks (#92)
* delete mem allocated for arguments * delete temp arrays * adding nullptr checks
1 parent b89e6b2 commit 93f134b

File tree

6 files changed

+41
-41
lines changed

6 files changed

+41
-41
lines changed

src/CollComm.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
namespace SHARPY {
66

77
void bufferize(NDArray::ptr_type a_ptr, void *outPtr) {
8+
if (!outPtr)
9+
return;
810
dispatch(a_ptr->dtype(), a_ptr->data(), [&a_ptr, outPtr](auto *ptr) {
911
auto buff = static_cast<decltype(ptr)>(outPtr);
1012
auto shp = a_ptr->local_shape();

src/NDArray.cpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ NDArray::NDArray(id_type guid_, DTypeId dtype_, const shape_type &shp,
5757
}
5858
_lData = DynMemRef(nds, allocated, allocated, 0, sizes, strides);
5959
assert(team() == 0 || transceiver() == getTransceiver());
60+
delete[] sizes;
61+
delete[] strides;
6062
}
6163

6264
// incomplete, useful for computing meta information
@@ -232,37 +234,6 @@ int64_t NDArray::__int__() const {
232234
return res;
233235
}
234236

235-
void NDArray::add_to_args(std::vector<void *> &args) const {
236-
int ndims = this->ndims();
237-
auto storeMR = [ndims](const DynMemRef &mr) -> intptr_t * {
238-
intptr_t *buff = new intptr_t[memref_sz(ndims)];
239-
buff[0] = reinterpret_cast<intptr_t>(mr._allocated);
240-
buff[1] = reinterpret_cast<intptr_t>(mr._aligned);
241-
buff[2] = static_cast<intptr_t>(mr._offset);
242-
memcpy(buff + 3, mr._sizes, ndims * sizeof(intptr_t));
243-
memcpy(buff + 3 + ndims, mr._strides, ndims * sizeof(intptr_t));
244-
return buff;
245-
}; // FIXME memory leak?
246-
247-
if (team() == 0 || ndims == 0) {
248-
// no-dist-mode
249-
args.push_back(storeMR(_lData));
250-
} else {
251-
args.push_back(storeMR(_lhsHalo));
252-
args.push_back(storeMR(_lData));
253-
args.push_back(storeMR(_rhsHalo));
254-
// local offsets last
255-
auto buff = new intptr_t[memref_sz(1)];
256-
assert(5 == memref_sz(1));
257-
buff[0] = reinterpret_cast<intptr_t>(_lOffsets.data());
258-
buff[1] = buff[0];
259-
buff[2] = 0;
260-
buff[3] = ndims;
261-
buff[4] = 1;
262-
args.push_back(buff);
263-
}
264-
}
265-
266237
void NDArray::replicate() {
267238
if (is_replicated())
268239
return;

src/idtr.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ inline SHARPY::id_type get_guid() { return ++_nguid; }
2929
// Transceiver * theTransceiver = MPITransceiver();
3030

3131
template <typename T> T *mr_to_ptr(void *ptr, intptr_t offset) {
32+
if (!ptr) {
33+
throw std::runtime_error("Fatal: cannot handle offset on nullptr");
34+
}
3235
return reinterpret_cast<T *>(ptr) + offset;
3336
}
3437

@@ -91,7 +94,7 @@ uint64_t idtr_nprocs(SHARPY::Transceiver *tc) {
9194
initMPIRuntime();
9295
tc = SHARPY::getTransceiver();
9396
#endif
94-
return tc->nranks();
97+
return tc ? tc->nranks() : 1;
9598
}
9699
#pragma weak _idtr_nprocs = idtr_nprocs
97100
#pragma weak _mlir_ciface__idtr_nprocs = idtr_nprocs
@@ -102,7 +105,7 @@ uint64_t idtr_prank(SHARPY::Transceiver *tc) {
102105
initMPIRuntime();
103106
tc = SHARPY::getTransceiver();
104107
#endif
105-
return tc->rank();
108+
return tc ? tc->rank() : 0;
106109
}
107110
#pragma weak _idtr_prank = idtr_prank
108111
#pragma weak _mlir_ciface__idtr_prank = idtr_prank
@@ -226,6 +229,9 @@ mlir2sharpy(const ::imex::ndarray::DType dt) {
226229
void bufferize(void *cptr, SHARPY::DTypeId dtype, const int64_t *sizes,
227230
const int64_t *strides, const int64_t *tStarts,
228231
const int64_t *tSizes, uint64_t nd, uint64_t N, void *out) {
232+
if (!cptr || !sizes || !strides || !tStarts || !tSizes) {
233+
return;
234+
}
229235
dispatch(dtype, cptr,
230236
[sizes, strides, tStarts, tSizes, nd, N, out](auto *ptr) {
231237
auto buff = static_cast<decltype(ptr)>(out);
@@ -252,6 +258,9 @@ void bufferize(void *cptr, SHARPY::DTypeId dtype, const int64_t *sizes,
252258
void unpack(void *in, SHARPY::DTypeId dtype, const int64_t *sizes,
253259
const int64_t *strides, const int64_t *tStarts,
254260
const int64_t *tSizes, uint64_t nd, uint64_t N, void *out) {
261+
if (!in || !sizes || !strides || !tStarts || !tSizes || !out) {
262+
return;
263+
}
255264
dispatch(dtype, out, [sizes, strides, tStarts, tSizes, nd, N, in](auto *ptr) {
256265
auto buff = static_cast<decltype(ptr)>(in);
257266

@@ -276,6 +285,9 @@ template <typename T>
276285
void copy_(uint64_t d, uint64_t &pos, T *cptr, const int64_t *sizes,
277286
const int64_t *strides, const uint64_t *chunks, uint64_t nd,
278287
uint64_t start, uint64_t end, T *&out) {
288+
if (!cptr || !sizes || !strides || !chunks || !out) {
289+
return;
290+
}
279291
auto stride = strides[d];
280292
uint64_t sz = sizes[d];
281293
uint64_t chunk = chunks[d];
@@ -311,6 +323,9 @@ void copy_(uint64_t d, uint64_t &pos, T *cptr, const int64_t *sizes,
311323
void bufferizeN(void *cptr, SHARPY::DTypeId dtype, const int64_t *sizes,
312324
const int64_t *strides, const int64_t *tStarts,
313325
const int64_t *tEnds, uint64_t nd, uint64_t N, void *out) {
326+
if (!cptr || !sizes || !strides || !tStarts || !tEnds || !out) {
327+
return;
328+
}
314329
std::vector<uint64_t> chunks(nd);
315330
chunks[nd - 1] = 1;
316331
for (uint64_t i = 1; i < nd; ++i) {
@@ -377,6 +392,10 @@ void _idtr_reshape(SHARPY::DTypeId sharpytype, int64_t lRank,
377392
initMPIRuntime();
378393
tc = SHARPY::getTransceiver();
379394
#endif
395+
if (!gShapePtr || !lDataPtr || !lShapePtr || !lStridesPtr || !lOffsPtr ||
396+
!oGShapePtr || !oDataPtr || !oShapePtr || !oOffsPtr || !tc) {
397+
throw std::runtime_error("Fatal: received nullptr in reshape");
398+
}
380399

381400
assert(std::accumulate(&gShapePtr[0], &gShapePtr[lRank], 1,
382401
std::multiplies<int64_t>()) ==
@@ -730,6 +749,13 @@ void *_idtr_update_halo(SHARPY::DTypeId sharpytype, int64_t ndims,
730749
initMPIRuntime();
731750
tc = SHARPY::getTransceiver();
732751
#endif
752+
753+
if (!ownedOff || !ownedShape || !ownedStride || !bbOff || !bbShape ||
754+
!ownedData || !leftHaloShape || !leftHaloStride || !leftHaloData ||
755+
!rightHaloShape || !rightHaloStride || !rightHaloData || !tc) {
756+
throw std::runtime_error("Fatal error: received nullptr in update_halo.");
757+
}
758+
733759
auto nworkers = tc->nranks();
734760
if (nworkers <= 1 || getenv("SHARPY_SKIP_COMM"))
735761
return nullptr;
@@ -824,6 +850,11 @@ void *_idtr_update_halo(SHARPY::Transceiver *tc, int64_t gShapeRank,
824850
void *bbShapeDescr, int64_t lHaloRank, void *lHaloDescr,
825851
int64_t rHaloRank, void *rHaloDescr, int64_t key) {
826852

853+
if (!gShapeDescr || !oOffDescr || !oDataDescr || !bbOffDescr ||
854+
!bbShapeDescr || !lHaloDescr || !rHaloDescr) {
855+
throw std::runtime_error("Fatal error: received nullptr in update_halo.");
856+
}
857+
827858
auto sharpytype = SHARPY::DTYPE<T>::value;
828859

829860
// Construct unranked memrefs for metadata and data

src/include/sharpy/NDArray.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,6 @@ class NDArray : public array_i, protected ArrayMeta {
187187
/// @return size of one element in number of bytes
188188
virtual int item_size() const override { return sizeof_dtype(_dtype); }
189189

190-
/// add array to list of args in the format expected by MLIR
191-
/// assuming array has ndims dims.
192-
virtual void add_to_args(std::vector<void *> &args) const override;
193-
194190
/// @return locally owned data as DynMemref
195191
const DynMemRef &owned_data() const { return _lData; }
196192
/// @return left halo as DynMemref

src/include/sharpy/array_i.hpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,6 @@ class array_i {
122122

123123
// size of a single element (in bytes)
124124
virtual int item_size() const = 0;
125-
// store array information in form of corresponding
126-
// jit::JIT::DistMemRefDescriptor
127-
// @return stored size in number of intptr_t
128-
virtual void add_to_args(std::vector<void *> &args) const = 0;
129125
// mark as deallocated
130126
virtual void markDeallocated() = 0;
131127
virtual bool isAllocated() = 0;

src/jit/mlir.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ void DepManager::finalizeAndRun() {
238238
if (osz > 0 || !input.empty()) {
239239
// compile and run the module
240240
auto output = _jit.run(_module, _fname, input, osz);
241+
// we assume we only store memrefdescriptors, e.g. arrays of inptr_t
242+
for (auto p : input) {
243+
delete[] reinterpret_cast<intptr_t *>(p);
244+
}
241245
if (output.size() != osz)
242246
throw std::runtime_error("failed running jit");
243247

0 commit comments

Comments
 (0)