I'm trying to solve constraint optimization problems under multi-threads. For example, solving the constraint optimization for period 1 in thread 1 and solving for period 2 in thread 2.When solving for constraint optimization, I define constraint using _co_EqProc and define gradient using _co_GradProc. But in multi-threading, we cannot assign the same symbol in two threads. Does that mean I cannot assign _co_EqProc in both thread 1 and thread 2? If this is the case, how can I solve a bunch of optimization problem using multi-threads?
The original GAUSS application module, Constrained Optimization (CO), is not threadsafe, as you point out. That is the reason that the MT or multithread safe versions of the application modules were developed. To run more than one constrained optimization problem at a time in multiple threads, you need to use Constrained Optimization MT (COMT)
Thanks for your reply.
I'm interested in COMT package, but want to make sure this package can help me solving the problem I'm facing.
I have T periods of constraint optimization problem to solve. I'm thinking about using multi-threading in the way such that solving period 1 to T/3 in thread 1, period T/3+1 to 2T/3 in thread 2 and period 2T/3+1 to T in thread 3. Will the package work?
All you have to do is make sure that the call to COMT in each thread has it's own input and output arguments. Thus
struct DS d1;
struct DS d2;
struct DS d3;
d1.dataMatrix = data[1:t1,.];
d1 = dsCreate;
d2.dataMatrix = data[t1+1:t2,.];
d2 = dsCreate;
d3.dataMatrix = data[d2+1:rows(data),.];
d3 = dsCreate;
struct PV p1;
p1 = pvCreate;
struct PV p2;
p2 = pvCreate;
struct PV p3;
p3 = pvCreate;
p1 = pvPack(p1,1|1|1|1,"x");
p2 = pvPack(p2,1|1|1|1,"x");
p3 = pvPack(p3,1|1|1|1,"x");
struct comtControl c1;
struct comtControl c2;
struct comtControl c3;
c1 = comtControlCreate;
c2 = comtControlCreate;
c3 = comtControlCreate;
c1.ineqProc = &ineq1;
c2.ineqProc = &ineq2;
c3.ineqProc = &ineq3;
proc ineq(struct PV p, struct DS d);
x = pvUnpack(p1,"x");
retp(x - x); // constraints x > x
proc fct(struct PV p, struct DS d, ind);
struct modelResults m;
x = pvUnpack(p1,"x");
m.function = ... // objective function
struct comtResults out1;
struct comtResults out2;
struct comtResults out3;
out1 = COMT(&fct,p1,d1,c1);
out2 = COMT(&fct,p2,d2,c2);
out3 = COMT(&fct,p3,d3,c3);
print "coefficients from first part of data " pvGetParVector(out1.par);
print "coefficients from second part of data " pvGetParVector(out2.par);
print "coefficients from third part of data " pvGetParVector(out3.par);
Thanks for the reply.
What about threads on a multi-core processor, e.g. to create a program that uses two different threads that are executed by two different processor cores? Will it be managed automatically?
There are a few different ways that GAUSS can use separate processor cores in this situation.
1. GAUSS launches a new thread to run all code between threadBegin and threadEnd statement. So if you run:
threadBegin; out1 = COMT(&fct,p1,d1,c1); threadEnd; threadBegin; out2 = COMT(&fct,p2,d2,c2); threadEnd; threadJoin;
each of the calls to COMT will be run on a separate thread. Your operating system will handle the details of which core to run it on. With standard settings, your operating system will send the threads out to any available processor cores.
2. Parts of the COMT code can create multiple threads internally. this is controlled by the useThreads member of the COMT control structure.
If you leave this setting on, then you will create two threads (one for each COMT call) and each of these threads will create another four or so. Each time you run the same code in more than one thread at the same time, you increase the memory needs of your program quite a bit.
Therefore, it is recommended to turn off the threading inside of COMT the first time you run your program. If your system seems to have plenty of resources (memory and cpu cores) available while it is running your program, then you may try turning useThreads on.
What about the speed? If I run two threads on one core, would Gauss wind up running each thread half as fast?
Your operating system handles the thread scheduling. It will send the threads to different cores if you have any available. It will never pin two threads on one core if you have more available (unless you have explicitly set the thread affinity this way). You do not have to concern yourself with this detail.
I'm trying to write an objective function when using comt. For example,
proc fct(struct PV p, struct DS d, ind)
My question is how to assign value to the third inputs 'ind'. Is it through comtControl?
This second question about the ind input was moved to a separate question to make it easier for other users to search and find this answer. You can view it here.
If I want to run many constrained optimization problem at a time in multiple threads, say I want to solve 10 problems in 10 threads, do I need to write them one by one?
Is there easy way to write them? For example, can I write it in a way like loop?
I called a procedure using COMT in two threads. After the procedure, I wanted to print some parameters. But the output is strange. I saw two lines of output which are exactly the same. What might cause this problem?