I am trying to solve a nonlinear system using Ceres Solver by Google. The example below comes from this page: http://terpconnect.umd.edu/~petersd/460/html/newtonex1z.html
I first create a class called MatlabExample, where I compute the residuals and jacobians:
class MatlabExample
: public SizedCostFunction<2,2> {
public:
virtual ~MatlabExample() {}
virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const {
double x1 = parameters[0][0];
double x2 = parameters[0][1];
residuals[0] = 2*x1+x1*x2-2;
residuals[1] = 2*x2-x1*pow(x2,2)-2 ;
if (jacobians != NULL && jacobians[0] != NULL) {
jacobians[0][0] = 2+x2;
jacobians[0][1] = x1;
jacobians[1][0] = -pow(x2,2);
jacobians[1][1] = 2-2*x1*x2;
}
return true;
}
};
The main file is as follows:
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
double x[] = { 0.0,0.0 };
Problem problem;
CostFunction* cost_function = new MatlabExample;
problem.AddResidualBlock(cost_function, NULL, &x);
Solver::Options options;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary);
std::cout << summary.BriefReport() << "\n";
return 0;
}
When compiling, I got a Segmentation fault: 11 error. Any ideas?
You are accessing the jacobians array wrong. Here is why.
When you added the residual block, you told Ceres that the cost function only depends on one parameter block of size 2 and produces a residual of size 2.
The jacobians array is an array of row-major jacobians. One for each parameter block. So in this case it is of size 1, and contains a pointer to size 4 array that should contain a row major Jacobian.
Your Jacobian filling code should instead read
if (jacobians != NULL && jacobians[0] != NULL) {
jacobians[0][0] = 2+x2;
jacobians[0][1] = x1;
jacobians[0][2] = -pow(x2,2);
jacobians[0][3] = 2-2*x1*x2;
}
Related
I am tasked with writing a program to maintain the representation of a simple network(weighted directed graph) and compute the best path between two given nodes upon request.
Currently, I am attempting to write a function to compute the simplest between two nodes, however, when attempting to run my program, I get two specific error
Severity Code Description Project File Line Suppression State
Error C3863 array type 'bool [openNode]' is not assignable P 127
and
Severity Code Description Project File Line Suppression State
Error C3863 array type 'int [openNode]' is not assignable
I am unable to debug since these two primary errors are not allowing my program to run. Is there any particular reason for these errors?
Thanks in advance!
This is the node structure defined in Graph.h
struct GraphNode
{
char ID;
std::string name;
int inNodes = 0;
int outNodes = 0;
std::vector<std::pair<GraphNode*, int>> connection;
int connections = 0;
};
And here is the particular code that causes the errors.
#include "Graph.h"
std::vector<GraphNode*> _graph;
int openNode = 0;
//Obligatory constructor
void Graph()
{
}
void shortestPath(char fromNode, char toNode)
{
bool known[openNode];
int distance[openNode];
GraphNode* previous[openNode];
int numbChecked = 0;
for (int i = 0; i < openNode; i++)
{
known[i] = false;
distance[i] = 999999;
previous[i] = nullptr;
}
distance[findNode(fromNode)] = 0;
while (numbChecked < openNode)
{
int smallestUnknown = 9999999;
int locationOfSmall = 0;
for (int i = 0; i < openNode; i++)
{
if (known[i] == false && distance[i] < smallestUnknown)
{
smallestUnknown = distance[i];
locationOfSmall = i;
}
}
if (distance[locationOfSmall] == 0)
{
previous[locationOfSmall] = nullptr;
}
known[locationOfSmall] = true;
numbChecked++;
if (_graph[locationOfSmall]->outNodes > 0)
{
for (int i = 0; i < _graph[locationOfSmall]->outNodes; i++)
{
int newDistanceLocation = findNode(_graph[locationOfSmall]->connection[i].first->ID);
if (known[newDistanceLocation] == false && (distance[locationOfSmall] + _graph[locationOfSmall]->connection[i].second) < distance[newDistanceLocation])
{
distance[newDistanceLocation] = distance[locationOfSmall] + _graph[locationOfSmall]->connection[i].second;
previous[newDistanceLocation] = _graph[locationOfSmall];
}
}
}
}
int destination = findNode(toNode);
std::string output;
std::string charTransfer;
charTransfer = toNode;
output = charTransfer;
while (previous[destination] != nullptr)
{
destination = findNode(previous[destination]->ID);
charTransfer = _graph[destination]->ID;
output = charTransfer + "->" + output;
}
if (_graph[destination]->ID != fromNode)
{
std::cout << "The nodes are not connected." << std::endl;
}
else
{
std::cout << "The path is: " << output << std::endl;
std::cout << "The distance is: " << distance[findNode(toNode)] << std::endl;
}
}
Any change suggestions would be much appreciated!
You have invalid code at the beginning of your shortestPath function:
bool known[openNode];
int distance[openNode];
GraphNode* previous[openNode];
You cannot use variables to create arrays on the stack (which is what you are trying to do there), because the compiler doesn't know the value of openNode at compile time (which is needed to determine the stack size).
Why don't you use a vector, like:
std::vector<bool> known(openNode, false);
std::vector<int> distance(openNode, 999999);
std::vector<GraphNode*> previous(openNode, nullptr);
Using this method makes the for loop below obsolete aswell.
The problem is as described above. When I try to read values from loaded *.so file (using libdl), whih are in struct I am getting wrong values
Code of application:
#include <dlfcn.h>
#include <iostream>
/* For face data type reproduction */
#define GET_FACE_XYZ_SIZE 1
/* For face_array reproduction */
#define GET_FACE_ARRAY_SIZE 2
#define GET_OBJECT_DATA 3
typedef struct face {
float x[1000];
float y[1000];
float z[1000];
int vertices;
} face;
int main()
{
void *hook;
int (*fn)(int request_type, void *ptr);
hook = dlopen("/root/osms/dlopen-test/lib.so", RTLD_LAZY);
if(!hook)
{
std::cout << "Couldn't find lib.so" << std::endl;
}
fn = dlsym(hook, "object_info");
int face_array_size = fn(GET_FACE_ARRAY_SIZE, NULL);
std::cout << "FACE_ARRAY_SIZE: " << face_array_size << std::endl;
face pointer[face_array_size];
fn(NULL, pointer);
dlclose(hook);
std::cout << "pointer[0].z[1]: " << pointer[0].z[1] << std::endl;
return 0;
}
and code of lib.so:
/* For face data type reproduction */
#define GET_FACE_XYZ_SIZE 1
/* For face array reproduction */
#define GET_FACE_ARRAY_SIZE 2
#define GET_OBJECT_DATA 3
typedef struct face {
float x[1000];
float y[1000];
float z[1000];
int vertices;
} face;
extern "C" int object_info(int request, void *ptr)
{
face face_array[2];
face_array[0].x[0] = 1.1;
face_array[0].y[0] = 0.5;
face_array[0].z[0] = 1.2;
face_array[0].x[1] = 1.6;
face_array[0].y[1] = -0.11;
face_array[0].z[1] = -12;
face_array[0].x[2] = -0.12;
face_array[0].y[2] = 0.24;
face_array[0].z[2] = -0.12;
face_array[0].vertices = 3;
face_array[1].x[0] = -1.1;
face_array[1].y[0] = 0.15;
face_array[1].z[0] = -1.2;
face_array[1].x[1] = -1.6;
face_array[1].y[1] = 0.11;
face_array[1].z[1] = 1.2;
face_array[1].x[2] = 0.12;
face_array[1].y[2] = -0.24;
face_array[1].z[2] = 0.12;
face_array[1].vertices = 3;
if(request == GET_FACE_ARRAY_SIZE)
{
return 2;
}
else
{
ptr = face_array;
}
}
The expected output is pointer[0].z[1]: -12 but I am getting pointer[0].z[1]: -0.12. What's wrong in my code ?
Thanks in advance
Accessing
pointer[0].z[1]
Has undefined behaviour, because it has an indeterminate value.
object_info never modifies the array pointed by ptr. It simply modifies a local array, and assigns the local ptr to point to that local array.
A solution: Don't declare a local array, and instead modify the array pointed by the argument. In other words, repace face face_array[2]; with:
face* face_array = (face*)ptr;
And get rid of the ptr = face_array; that does nothing meaningful.
object_info is declared to return int, but not all code paths return a value. When the function reaches the end of object_info without a return statement, the behaviour is undefined.
A solution: Always return a value if the function is not void.
face_array_size is not a compile time constant value, so face pointer[face_array_size]; will declare a variable length array. VLA are not allowed in C++.
Either use C (VLA are supported since C99, but only optionally supported since C11) instead or use a dynamic array: std::vector<face> or make peace with the fact that your program is not standard compliant.
The variable "face_array" in function object_info and the variable "pointer" in main are not the same variable.
The statement "ptr = face_array" does not change the content of "pointer".
extern "C" int object_info(int request, face *face_array)
{
if(request == GET_FACE_ARRAY_SIZE)
return 2;
face_array[0].x[0] = 1.1;
face_array[0].y[0] = 0.5;
face_array[0].z[0] = 1.2;
face_array[0].x[1] = 1.6;
face_array[0].y[1] = -0.11;
face_array[0].z[1] = -12;
face_array[0].x[2] = -0.12;
face_array[0].y[2] = 0.24;
face_array[0].z[2] = -0.12;
face_array[0].vertices = 3;
face_array[1].x[0] = -1.1;
face_array[1].y[0] = 0.15;
face_array[1].z[0] = -1.2;
face_array[1].x[1] = -1.6;
face_array[1].y[1] = 0.11;
face_array[1].z[1] = 1.2;
face_array[1].x[2] = 0.12;
face_array[1].y[2] = -0.24;
face_array[1].z[2] = 0.12;
face_array[1].vertices = 3;
}
I need to interpolate a modified Bessel function and I use for that the function gsl_spline_eval(). I get a 'gsl: interp.c:145: ERROR: interpolation error' with the following code:
#define MAXPOINTS 10000
int main()
{
double nu;
gsl_function F;
F.function = &sync;
F.params =0;
size_t size;
double table[2][MAXPOINTS];
size = read_table(table[0],table[1]);
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_linear, 5401);
gsl_spline_init(spline,table[0],table[1],size);
cout <<" nu: "<< nu<< " GSL_FN_EVAL(&F,nu): "<<nu*GSL_FN_EVAL(&F,nu)<<endl;
return 0;
}
Where sync() returns the integral of the function sync_kern() using the gsl function gsl_integration_qag(). The function read_table() is defined as follow:
size_t read_table(double *xa, double *ya)
{
size_t datapoints;
datapoints = 5401;
double x[] = {
1.388794e-11,
1.395756e-11,
... };
double y[] = {
5.166810e-04,
5.175428e-04,
...
};
int i = 0;
while(i<datapoints){
xa[i]=log(x[i]);
ya[i]=log(y[i]);
i++;
}
return datapoints;
}
The function sync_kern() is the following:
double sync_kern(double gamma, void *params)
{
struct func_params *part= (struct func_params *)params;
double result;
double P,x, nuc, nu_0;
double nu = *(double *)params;
gamma = exp(gamma);
nu_0 = (3*E_COULOMB*B*sqrt(2/3.0))/(4.0*M_PI*M*C);
nuc = nu_0*sq(gamma);
x = nu/nuc;
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_linear, 5401);
/* double table[2][MAXPOINTS];
size = read_table(table[0],table[1]);
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_interp, 5401);
gsl_spline_init(spline,table[0],table[1],size); */
P = gsl_spline_eval(spline,log(x),acc);
result = exp(P)*f(gamma)*gamma;
return(result);
}
When I include the lines
/* double table[2][MAXPOINTS];
size = read_table(table[0],table[1]);
gsl_interp_accel *acc = gsl_interp_accel_alloc ();
gsl_spline *spline = gsl_spline_alloc (gsl_interp_interp, 5401);
gsl_spline_init(spline,table[0],table[1],size); */
in the function sync_kern() and not in main() then it works: I get the right value of GSL_FN_EVAL(&F,nu) but it takes of course too much time...
I hope my message wasn't too long...I am not a great expert of c++ and I've look all the aspects of the problem and I still don't understand where it comes from...Does someone have any idea?
Please don't hesitate do ask me if you need any further informations.
Thanks a lot for your help!
Maybe I should precise that I got my code from my supervisor's code. Which is slightly different:
int main(int argc,char *argv[])
{
FILE *fp;
struct func_params params;
size_t size;
int i;
/* initializing parameters */
initialize(argc, argv, ¶ms);
/* Interpolation of Synchrotronfunction F(x) */
double table[2][MAXPOINTS];
size = read_table(table[0],table[1]); /*Tabelle fuer Interpolation einlesen aus create_table.c*/
params.acc = gsl_interp_accel_alloc();
params.spline = gsl_spline_alloc(gsl_interp_linear, size);
gsl_spline_init(params.spline,table[0],table[1],size);
...
return (0);
}
double
sync_kern(double gamma, void *params)
{
struct func_params *part= (struct func_params *)params;
double result;
double P,nu_c,x;
gamma = exp(gamma);
nu_c = part->nu_0*sq(gamma);
x = part->nu_s/nu_c;
P = gsl_spline_eval(part->spline,log(x),part->acc); /*Aus Interpolation: Pointer auf spline */
result = exp(P)*f(gamma)*gamma;
return(result);
}
Where spline and acc are defined in a file.h:
struct func_params
{
gsl_spline *spline;
gsl_interp_accel *acc;
...
}
void initialize(int argc, char *argv[], void *params)
{
struct func_params *part= (struct func_params *)params;
/* Default values for parameters: */
part->gmin=1.0e7/M_E_EV;
part->gamma=1.0e10/M_E_EV;
part->gmax=2.0e18/M_E_EV; /*Integrationgrenzen fuer gamma festlegen*/
part->nu=1e-9/H_EV;
...
}
I defined spline and acc in my function sync_kern() and him in a structure. Does someone has an idea where the error can come from?
The function read_table() gives the arrays 'x[]' and 'y[]' used for
the interpolation
double table[2][MAXPOINTS];
size = read_table(table[0],table[1]);
You define an array of doubles, but don't initialize it. It contains random values. So likely it runs with absurd results.
You pass two values to read_table(). If you want the function to change them, the definition of this function should be
size_t read_table(double& val1, double& val2);
Review your code. There are some vars without declaration (nu_0, nuc, acc)
EDIT
After you edited the question, I see how read_table() is defined. It uses two pointers. You have an only array, two dimensions, fixed size. Because of this fixed size, it's OK to pass the array by using two pointers, table[0] and table[1].
double table[2][N] is same as double** table. So passing table[1] is passing a pointer to an array[N].
The gsl error is due to this code at gsl:interp.c
https://github.com/ampl/gsl/blob/master/interpolation/interp.c
if (x < interp->xmin || x > interp->xmax)
{
GSL_ERROR_VAL("interpolation error", GSL_EDOM, GSL_NAN);
}
It seems you are trying an interpolation out of limits (called "extrapolation")
i am currently learning to use openmpi, my aim is to parallelize a simple program whose code i will post bellow.
The program is for testing my concept of paralleling a much bigger program, i hope to learn all i need to know for my actual problem if i succeed with this.
Basically it is a definition of a simple c++ class for lists. A list consists of two arrays, one integer and one double. Entries with the same indicies belong together, in a way that the integer entry is some kind of list entry identifier (maybe an object ID) and the double entry is some kind of quantifier (maybe the weight if an object).
The basic purpose of the program is to add lists together (this is the task i want to parallelize). Adding works as follows: For each entry in one list it is checked if there is the same integer entry in the the other list, if so then the double entry gets added to the double entry in the other list, if there is no such entry in the other list then both the integer and the double entries gets added to the end of the list.
Basically each summand in this list addition represents a storage and each entry is a type of object with a given amount (int is the type and double is the amount), so adding two lists means putting the stuff from the second storage to the first.
The order of the list entries is irrelevant, this means that the addition of lists is not only associative but commutative too!
My plan is to add a very large number of such lists (a few billions) so parallelizing could be to let each thread add a subset of lists first and when this is finished distribute all such sublists (one for each thread) to all of the threads.
My current understanding of openmpi is that only the last step (distributing of finished sublists) needs any special non standard stuff. Basically i need a AllReduce but with a custom data type and a custom operaton.
The first problem i have is understanding how to create a fitting MPI data type. I came to the conclusion that i probably need MPI_Type_create_struct to create a struct type.
I found this site with a nice example: http://mpi.deino.net/mpi_functions/MPI_Type_create_struct.html
from which i learned a lot but the problem is, that in this case there are fixed member arrays. In my case i have lists with arbitrary sized member variables or better with pointers pointing to memory blocks of arbitrary size. So doing it like in the example would lead to creating a new MPI datatype for each list size (using fixed sized lists could help but only in this minimalistic case, but i want to learn how to do it with arbitrary sized lists are preparation for my actual problem).
So my question is: how to create a data type for this special case? What is the best way?
I even thought to maybe write some non mpi code to serialize my class/object, (which would be a lot of work for my real problem but in this example it should be easy) to a single block of bits. Then i could simply use a MPI function to distribute those blocks to all threads and then i just have to translate it back to the actual object, and then i could let each thread simply add the "number-of-threads" lists together to have the same full reduced list on all threads (because the operation is commutative it is not important if the order is the same on each thread in the end).
The problem is that i do not know which MPI function to use to distribute a such memory blocks to each thread so that in the end each thread has an array of "number-of-threads" such blocks (similar like AllReduce but with blocks).
But thats just another idea, i would like to hear from you whats the best way.
Thank you, here is my fully working example program (ignore the MPI parts thats just preparation, you can simply compile with: g++)
As you can see, i needed to create custom copy constructors because standard of the pointer members. I hope thats not a problem for MPI?
#include <iostream>
#include <cstdlib>
#if (CFG_MPI > 0)
#include <mpi.h>
#else
#define MPI_Barrier(xxx) // dummy code if not parallel
#endif
class list {
private:
int *ilist;
double *dlist;
int n;
public:
list(int n, int *il, double *dl) {
int i;
if (n>0) {
this->ilist = (int*)malloc(n*sizeof(int));
this->dlist = (double*)malloc(n*sizeof(double));
if (!ilist || !dlist) std::cout << "ERROR: malloc in constructor failed!" << std::endl;
} else {
this->ilist = NULL;
this->dlist = NULL;
}
for (i=0; i<n; i++) {
this->ilist[i] = il[i];
this->dlist[i] = dl[i];
}
this->n = n;
}
~list() {
free(ilist);
free(dlist);
ilist = NULL;
dlist = NULL;
this->n=0;
}
list(const list& cp) {
int i;
this->n = cp.n;
this->ilist = NULL;
this->dlist = NULL;
if (this->n > 0) {
this->ilist = (int*)malloc(this->n*sizeof(int));
this->dlist = (double*)malloc(this->n*sizeof(double));
if (!ilist || !dlist) std::cout << "ERROR: malloc in copy constructor failed!" << std::endl;
}
for (i=0; i<this->n; i++) {
this->ilist[i] = cp.ilist[i];
this->dlist[i] = cp.dlist[i];
}
}
list& operator=(const list& cp) {
if(this == &cp) return *this;
this->~list();
int i;
this->n = cp.n;
if (this->n > 0) {
this->ilist = (int*)malloc(this->n*sizeof(int));
this->dlist = (double*)malloc(this->n*sizeof(double));
if (!ilist || !dlist) std::cout << "ERROR: malloc in copy constructor failed!" << std::endl;
} else {
this->ilist = NULL;
this->dlist = NULL;
}
for (i=0; i<this->n; i++) {
this->ilist[i] = cp.ilist[i];
this->dlist[i] = cp.dlist[i];
}
return *this;
}
void print() {
int i;
for (i=0; i<this->n; i++)
std::cout << i << " : " << "[" << this->ilist[i] << " - " << (double)dlist[i] << "]" << std::endl;
}
list& operator+=(const list& cp) {
int i,j;
if(this == &cp) {
for (i=0; i<this->n; i++)
this->dlist[i] *= 2;
return *this;
}
double *dl;
int *il;
il = (int *) realloc(this->ilist, (this->n+cp.n)*sizeof(int));
dl = (double *) realloc(this->dlist, (this->n+cp.n)*sizeof(double));
if (!il || !dl)
std::cout << "ERROR: 1st realloc in operator += failed!" << std::endl;
else {
this->ilist = il;
this->dlist = dl;
il = NULL;
dl = NULL;
}
for (i=0; i<cp.n; i++) {
for (j=0; j<this->n; j++) {
if (this->ilist[j] == cp.ilist[i]) {
this->dlist[j] += cp.dlist[i];
break;
}
} if (j == this->n) {// no matching entry found in this
this->ilist[this->n] = cp.ilist[i];
this->dlist[this->n] = cp.dlist[i];
this->n++;
}
}
il = (int *) realloc(this->ilist, (this->n)*sizeof(int));
dl = (double *) realloc(this->dlist, (this->n)*sizeof(double));
if (!il || !dl)
std::cout << "ERROR: 2nd realloc in operator += failed!" << std::endl;
else {
this->ilist = il;
this->dlist = dl;
}
return *this;
}
};
int main(int argc, char **argv) {
int npe, myid;
#if (CFG_MPI > 0)
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD,&npe);
MPI_Comm_rank(MPI_COMM_WORLD,&myid);
#else
npe=1;
myid=0;
#endif
if (!myid) // reduce output
std::cout << "NPE = " << npe << " MYID = " << myid << std::endl;
int ilist[5] = {14,17,4,29,0};
double dlist[5] = {0.0, 170.0, 0.0, 0.0, 24.523};
int ilist2[6] = {14,117,14,129,0, 34};
double dlist2[6] = {0.5, 170.5, 0.5, 0.5, 24.0, 1.2};
list tlist(5, ilist, dlist);
list tlist2(6, ilist2, dlist2);
if (!myid) {
tlist.print();
tlist2.print();
}
tlist +=tlist2;
if (myid) tlist.print();
#if (CFG_MPI > 0)
MPI_Finalize();
#endif
return 0;
}
I used to work with 3D dynamic allocated arrays but I read that 1D array has better performance so I tried to transorm my 3D array in a 1D array.
I work with two big arrays (about 240*320*1000 double) and the allocation look like this :
int numberFiles; //about 1000, can change
int nRow; //240
int nCol; //320
double *inputData;
double *outputData;
inputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always works
outputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always fails
The second allocation always fail. Since the first doesn't, it's not a size problem. And since, when allocate this in a 3D arrays, it's also working, it's not a memory space problem
Any ideas why the second allocation fails ?
Thanks.
Chen Song.
PS : Sorry for the poor english.
PS2: Tried new Double[], same problems.
Edit : So allocation in a simple code works but as soon as I try to put it in my app, it's not working anymore. I'm woking with QtCreator with mingw32 and have matio(libraries to read MATLAB files) to read data. Here a code example of my program which give me error :
//Needed include
using namespace std;
int main(int argc, char ** argv)
{
int k,i,j;
double *matriceBrute,*matriceSortie;
double* matData;
const char * c;
mat_t *matfp;
matvar_t *matvar;
QApplication app(argc,argv);
// Getting the directory of the data and the files in it
QString directoryPath = QFileDialog::getExistingDirectory();
QDir myDir(directoryPath);
myDir.setNameFilters(QStringList()<<"*.MAT");
QStringList filesList = myDir.entryList(QDir::Files | QDir::Hidden);
string fullPath= (directoryPath+"/"+filesList[0]).toLocal8Bit().constData();
c = fullPath.c_str();
// Loading the first one to get the size of the data
matfp = Mat_Open(c,MAT_ACC_RDONLY);
matvar = Mat_VarReadNextInfo(matfp);
int read_err = Mat_VarReadDataAll(matfp,matvar);
int numberFiles = (int)filesList.size();
int nCol = matvar->dims[1];
int nRow = matvar->dims[0];
//Allocating
matriceBrute = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
matriceSortie = (double*)malloc(nCol*nRow*numberFiles*sizeof(double));
//the first one works but not the second
//loading the data in the allocated memory
for (k=0;k<numberFiles;k++)
{
fullPath= (directoryPath+"/"+filesList[k]).toLocal8Bit().constData();
c = fullPath.c_str();
matfp = Mat_Open(c,MAT_ACC_RDONLY);
matvar = Mat_VarReadNext(matfp);
matData = (double*)(matvar->data);
for (i=0;i<nRow;i++)
{
for (j=0;j<nCol;j++)
{
matriceBrute[i + nRow*(j+k*nCol)]=*(matData+(i+j*nRow));
matriceSortie[i + nRow*(j+k*nCol)]=*(matData+(i+j*nRow));
}
}
Mat_VarFree(matvar);
Mat_Close(matfp);
}
}
No error, it works.
#include <iostream>
using namespace std;
int main()
{
int numberFiles = 1000; //about 1000, can change
int nRow = 240; //240
int nCol = 240; //320
double *inputData;
double *outputData;
inputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always works
outputData = (double*)malloc(nCol*nRow*numberFiles*sizeof(double)); //Always fails
if (inputData == NULL)
cout << "inputData alloc failed" << endl;
if (outputData == NULL)
cout << "outputData alloc failed" << endl;
return 0;
}
It does not print as failed, so, don't worry, be happy.
May it be memory fragmentation problem? If, for example, you can't alloc ~0.6Gb (by one heap) twice? First time you alloc it, at second - it just can't find in your physical memory such big heap of free memory?
Which exception do you receive?