Skip to content

Commit f6a1379

Browse files
committed
Fixed: 64bit issues, encapsulation issues, memory leaks, makefile commands.
64b issues: int cannot be used for dimensions passed to mex commands on 64bit systems. size_t can and should be valid also for 32bit. encapsulation: in release more, CKernel pkern is private in the CGp class. So I made a const getter to access it. memory leaks: unnecessary clone()-s for CTransform. makefile: added commands to compile objects for CGp, CIvm & ndlstrutil to give them all necessary flags.
1 parent 3027c51 commit f6a1379

14 files changed

+45
-35
lines changed

CDist.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ bool CDist::equals(const CDist& dist, double tol) const
4242
#ifdef _NDLMATLAB
4343
mxArray* CDist::toMxArray() const
4444
{
45-
int dims[1];
45+
size_t dims[1];
4646
dims[0] = 1;
4747
const char *fieldNames[] = {"type", "transforms"};
4848
mxArray* matlabArray = mxCreateStructArray(1, dims, 2, fieldNames);
@@ -285,7 +285,7 @@ double CWangDist::getGradInput(double x) const
285285
#ifdef _NDLMATLAB
286286
mxArray* CParamPriors::toMxArray() const
287287
{
288-
int dims[1];
288+
size_t dims[1];
289289
// dists field.
290290
const char *transFieldNames[] = {"index", "type"};
291291
dims[0]=getNumDists();

CGp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ CGp::CGp(CMatrix* pinData,
14351435
}
14361436
mxArray* CGp::toMxArray() const
14371437
{
1438-
int dims[1];
1438+
size_t dims[1];
14391439
dims[0]=1;
14401440
mxArray* matlabArray;
14411441
if(isSparseApproximation())

CGp.h

+3
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ class CGp : public CMapModel, public CProbabilisticOptimisable, public CStreamIn
345345
{
346346
backConstrained = val;
347347
}
348+
inline const CKern* getKernel() const {
349+
return pkern;
350+
}
348351

349352
CMapModel* backConstraintModel; // for mapping constraints on latent variables.
350353
CMatrix X_u; // for inducing variables if needed.

CIvm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ CIvm::CIvm(CMatrix* inData,
622622
}
623623
mxArray* CIvm::toMxArray() const
624624
{
625-
int dims[1];
625+
size_t dims[1];
626626
dims[0]=1;
627627
const char* fieldNames[]={"I", "J", "m", "beta"};
628628
mxArray* matlabArray = mxCreateStructArray(1, dims, 4, fieldNames);

CKern.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -4077,7 +4077,7 @@ void CKern::readParamsFromStream(istream& in)
40774077
#ifdef _NDLMATLAB
40784078
mxArray* CKern::toMxArray() const
40794079
{
4080-
int dims[1];
4080+
size_t dims[1];
40814081
dims[0] = 1;
40824082
const char *fieldNames[] = {"type", "transforms", "inputDimension"};
40834083
mxArray* matlabArray = mxCreateStructArray(1, dims, 3, fieldNames);
@@ -4228,7 +4228,7 @@ void CComponentKern::extractParamFromMxArray(const mxArray* matlabArray)
42284228
void CComponentKern::addParamToMxArray(mxArray* matlabArray) const
42294229
{
42304230
// Add comp field to mxArray.
4231-
int dims[1];
4231+
size_t dims[1];
42324232
mxAddField(matlabArray, "comp");
42334233
dims[0] = components.size();
42344234
mxArray* compArray = mxCreateCellArray(1, dims);

CMatrix.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ void CMatrix::toUnheadedStream(ostream& out) const
11731173
#ifdef _NDLMATLAB
11741174
mxArray* CMatrix::toMxArray() const
11751175
{
1176-
int dims[2];
1176+
size_t dims[2];
11771177
dims[0] = nrows;
11781178
dims[1] = ncols;
11791179
mxArray* matlabArray = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
@@ -1204,7 +1204,7 @@ void CMatrix::fromFullMxArray(const mxArray* matlabArray)
12041204
{
12051205
throw ndlexceptions::Error("mxArray does not have 2 dimensions.");
12061206
}
1207-
const int* dims = mxGetDimensions(matlabArray);
1207+
const size_t* dims = mxGetDimensions(matlabArray);
12081208
resize(dims[0], dims[1]);
12091209
double* matlabVals = mxGetPr(matlabArray);
12101210
dcopy_(ncols*nrows, matlabVals, 1, vals, 1);
@@ -1220,12 +1220,12 @@ void CMatrix::fromSparseMxArray(const mxArray* matlabArray)
12201220
{
12211221
throw ndlexceptions::Error("mxArray does not have 2 dimensions.");
12221222
}
1223-
const int* dims = mxGetDimensions(matlabArray);
1223+
const size_t* dims = mxGetDimensions(matlabArray);
12241224
resize(dims[0], dims[1]);
12251225
double* matlabVals = mxGetPr(matlabArray);
12261226
setVals(0.0);
1227-
int* matlabIr = mxGetIr(matlabArray);
1228-
int* matlabJc = mxGetJc(matlabArray);
1227+
size_t* matlabIr = mxGetIr(matlabArray);
1228+
size_t* matlabJc = mxGetJc(matlabArray);
12291229
int nnz = matlabJc[getCols()];
12301230
for(int j=0; j<getCols(); j++)
12311231
{

CNdlInterfaces.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ class CMatInterface
244244
}
245245
mxArray* convertMxArray(const bool val) const
246246
{
247-
int dims[1];
247+
size_t dims[1];
248248
dims[0] = 1;
249249
mxArray* matlabArray = mxCreateNumericArray(1, dims, mxDOUBLE_CLASS, mxREAL);
250250
double* matlabVals = mxGetPr(matlabArray);
@@ -256,7 +256,7 @@ class CMatInterface
256256
}
257257
mxArray* convertMxArray(const double val) const
258258
{
259-
int dims[1];
259+
size_t dims[1];
260260
dims[0] = 1;
261261
mxArray* matlabArray = mxCreateNumericArray(1, dims, mxDOUBLE_CLASS, mxREAL);
262262
double* matlabVals = mxGetPr(matlabArray);
@@ -269,7 +269,7 @@ class CMatInterface
269269
}
270270
mxArray* convertMxArray(const vector<int> vals) const
271271
{
272-
int dims[2];
272+
size_t dims[2];
273273
dims[0]=(int)vals.size();
274274
dims[1] = 1;
275275
mxArray* matlabArray = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
@@ -282,7 +282,7 @@ class CMatInterface
282282
}
283283
mxArray* convertMxArray(const vector<unsigned int> vals) const
284284
{
285-
int dims[2];
285+
size_t dims[2];
286286
dims[0]=(int)vals.size();
287287
dims[1] = 1;
288288
mxArray* matlabArray = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);

CNoise.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void CNoise::updateSites(CMatrix& m, CMatrix& beta, unsigned int actIndex,
5959
#ifdef _NDLMATLAB
6060
mxArray* CNoise::toMxArray() const
6161
{
62-
int dims[1];
62+
size_t dims[1];
6363
dims[0] = 1;
6464
const char *fieldNames[] = {"type", "transforms", "numProcess", "spherical", "nParams", "missing", "logconcave"};
6565

CTransform.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ bool CParamTransforms::equals(CParamTransforms transforms) const
158158
#ifdef _NDLMATLAB
159159
mxArray* CParamTransforms::toMxArray() const
160160
{
161-
int dims[1];
161+
size_t dims[1];
162162
// transforms field.
163163
const char *transFieldNames[] = {"index", "type"};
164164
dims[0]=getNumTransforms();

CTransform.h

+5-11
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,8 @@ class CParamTransforms : public CMatInterface, public CStreamInterface
172172
#endif
173173
void addTransform(const CTransform* trans, unsigned int index)
174174
{
175-
176175
transIndex.push_back(index);
177-
transforms.push_back(trans->clone());
176+
transforms.push_back(trans);
178177
}
179178

180179
void clearTransforms()
@@ -184,23 +183,20 @@ class CParamTransforms : public CMatInterface, public CStreamInterface
184183
}
185184
inline string getTransformType(unsigned int ind) const
186185
{
187-
188186
BOUNDCHECK(ind<getNumTransforms());
189187
return transforms[ind]->getType();
190188
}
191189
inline unsigned int getTransformIndex(unsigned int ind) const
192190
{
193-
194191
BOUNDCHECK(ind<getNumTransforms());
195192
return transIndex[ind];
196193
}
197194
inline unsigned int getNumTransforms() const
198195
{
199196
return transforms.size();
200197
}
201-
202198

203-
vector<CTransform*> transforms;
199+
vector<const CTransform*> transforms;
204200
vector<unsigned int> transIndex;
205201
};
206202

@@ -339,20 +335,18 @@ class CTransformable
339335
}
340336
void addTransform(const CTransform* trans, unsigned int index)
341337
{
342-
343338
BOUNDCHECK(index<getNumParams());
344-
transArray.transIndex.push_back(index);
345-
transArray.transforms.push_back(trans->clone());
339+
transArray.addTransform(trans, index);
346340
}
347-
341+
348342
void clearTransforms()
349343
{
350344
for(size_t i = 0; i<transArray.transforms.size(); i++)
351345
delete transArray.transforms[i];
352346
transArray.transIndex.clear();
353347
transArray.transforms.clear();
354348
}
355-
349+
356350
#ifdef _NDLMATLAB
357351
mxArray* transformsToMxArray() const
358352
{

gp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ void CClgp::learn()
412412
#ifdef _NDLMATLAB
413413
// Write matlab output.
414414
pmodel->writeMatlabFile(modelFileName, "gpInfo");
415-
pmodel->kern.updateMatlabFile(modelFileName, "kern");
415+
pmodel->getKernel()->updateMatlabFile(modelFileName, "kern");
416416
X.updateMatlabFile(modelFileName, "X");
417417
y.updateMatlabFile(modelFileName, "y");
418418
#else
@@ -508,7 +508,7 @@ void CClgp::relearn()
508508
#ifdef _NDLMATLAB
509509
// Write matlab output.
510510
pmodel->writeMatlabFile(newModelFileName, "gpInfo");
511-
pmodel->kern.updateMatlabFile(newModelFileName, "kern");
511+
pmodel->getKernel()->updateMatlabFile(newModelFileName, "kern");
512512
X.updateMatlabFile(newModelFileName, "X");
513513
y.updateMatlabFile(newModelFileName, "y");
514514
#else

gplvm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ void CClgplvm::learn()
600600
#ifdef _NDLMATLAB
601601
// Write matlab output.
602602
pmodel->writeMatlabFile(modelFileName, "gplvmInfo");
603-
pmodel->kern.updateMatlabFile(modelFileName, "kern");
603+
pmodel->pkern->updateMatlabFile(modelFileName, "kern");
604604
X.updateMatlabFile(modelFileName, "X");
605605
Y.updateMatlabFile(modelFileName, "Y");
606606
#else

ivm.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ void CClivm::relearn()
227227
#ifdef _NDLMATLAB
228228
// Write matlab output.
229229
model.writeMatlabFile(newModelFileName, "ivmInfo");
230-
model.kern.updateMatlabFile(newModelFileName, "kern");
231-
model.noise.updateMatlabFile(newModelFileName, "noise");
230+
model.pkern->updateMatlabFile(newModelFileName, "kern");
231+
model.pnoise->updateMatlabFile(newModelFileName, "noise");
232232
X.updateMatlabFile(newModelFileName, "X");
233233
y.updateMatlabFile(newModelFileName, "y");
234234
#else
@@ -649,8 +649,8 @@ void CClivm::learn() {
649649
#ifdef _NDLMATLAB
650650
// Write matlab output.
651651
model.writeMatlabFile(modelFileName, "ivmInfo");
652-
model.kern.updateMatlabFile(modelFileName, "kern");
653-
model.noise.updateMatlabFile(modelFileName, "noise");
652+
model.pkern->updateMatlabFile(modelFileName, "kern");
653+
model.pnoise->updateMatlabFile(modelFileName, "noise");
654654
X.updateMatlabFile(modelFileName, "X");
655655
y.updateMatlabFile(modelFileName, "y");
656656
#else

makefile

+13
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,23 @@ CMatrix.o: CMatrix.cpp CMatrix.h ndlassert.h ndlexceptions.h \
129129
CNdlInterfaces.h ndlstrutil.h ndlutil.h ndlfortran.h lapack.h
130130
$(CC) -c CMatrix.cpp -o CMatrix.o $(CCFLAGS)
131131

132+
CGp.o: CGp.cpp CGp.h CMltools.h ndlassert.h ndlexceptions.h \
133+
ndlstrutil.h COptimisable.h CMatrix.h CNdlInterfaces.h ndlutil.h \
134+
ndlfortran.h lapack.h CKern.h CTransform.h CDataModel.h CDist.h \
135+
CNoise.h
136+
$(CC) -c CGp.cpp -o CGp.o $(CCFLAGS)
137+
138+
CIvm.o: CIvm.cpp CIvm.h CMltools.h ndlassert.h ndlexceptions.h \
139+
ndlstrutil.h COptimisable.h CMatrix.h CNdlInterfaces.h ndlutil.h \
140+
ndlfortran.h lapack.h CKern.h CTransform.h CDataModel.h CDist.h \
141+
CNoise.h
142+
$(CC) -c CIvm.cpp -o CIvm.o $(CCFLAGS)
143+
132144
ndlutil.o: ndlutil.cpp ndlutil.h ndlassert.h ndlexceptions.h ndlfortran.h
133145
$(CC) -c ndlutil.cpp -o ndlutil.o $(CCFLAGS)
134146

135147
ndlstrutil.o: ndlstrutil.cpp ndlstrutil.h ndlexceptions.h
148+
$(CC) -c ndlstrutil.cpp -o ndlstrutil.o $(CCFLAGS)
136149

137150
ndlassert.o: ndlassert.cpp ndlassert.h ndlexceptions.h
138151
$(CC) -c ndlassert.cpp -o ndlassert.o $(CCFLAGS)

0 commit comments

Comments
 (0)