Creating an array takes lots of time
Awaiting user inputHi, i have an array ww[I, M, T] in which İ=3038, M=2 and T=84, it takes too long (5 minutes) to create this array, I checked this using breakpoint in my code. I do not understand why it takes so long. is there any method to fasten this? i define my array as given;
int[,,] wwArray = new int[I, M, T];
for (int i = 0; i < I; i++)
{
//Console.WriteLine(i);
for (int m = 0; m < M; m++)
{
for (int t = 0; t < T; t++)
{
int ww = 0;
for (int j = 0; j < J; j++)
{
for (int l = 0; l < L; l++)
{
ww += c[i, l] * ycap[m, j, l, t];
}
}
wwArray[i, m, t] = ww;
}
}
}
-
Hi Betül,
Thanks for posting on the forum! This seems to be a general performance issue unrelated to Gurobi, unless I'm missing something? Nevertheless I'm curious what the magnitude of J and L is here, since the total number of operations is O(I*M*T*J*L) those might help explain the slowness. Also, is your code written in C#? And what is the type of c and ycap - are they simple multidimensional arrays of type int[,] and int[,,,] as well?
Kind regards,
Ronald0 -
Hi Ronald, it seems it is not related with Gurobi you are right, but I have trouble with constructing the model so maybe I thought it can be related with Gurobi. Yes I am using c#.
In fact I also coded the model on gams and I have cplex licence on gams, I am solving the same model in seconds
c and ycap are simple multidimensional arrays of type int[,] and int[,,,] as well.
0 -
Interesting! Matrix multiplication in GAMS might be more efficient than "raw" C# - indeed this has nothing to do with the solver being used, whether that's Gurobi or CPLEX. But I'm still curious to see what can be done here :-)
- It would still be good to know the size of J and L.
- Also, I can imagine c and/or ycap are sparse, e.g. they contain many zeros. In this case, many of the operations done by .NET are effectively "useless". In that case I would go back to the source of the numbers for c and ycap and think of a different datastructure that would allow a more efficient calculation.
- Finally, you can always use multithreading to speed this calculation up. C# has low-level (e.g. Thread class) and more high-level (e.g. Task class, or even the Parallel.For options) features that could help you.
0 -
you are probably right, i am new with Gurobi and wondered if it is special for that.
* size of j and l are 225 both.
* yes you are right with this prediction, there are so many zeros e.g. in c there are 225*3038 elements and only 3038 of them have values where the rest is all zero. do you have any suggestion for the different data structure?
0 -
in c there are 225*3038 elements and only 3038 of them have values
I guess for every i there is exactly one l with a non-zero value? In that case:
- You don't need the inner-most loop iterating over all l , since only one is relevant
- c[,] could be one-dimensional instead
- You would need a dictionary that, for each i, gives you the corresponding l
Does that help? In theory it would give you a ~225x speedup to start with. You might be able to spot similar opportunities if ycap also has certain patterns.
0
Please sign in to leave a comment.
Comments
5 comments