Parallelizing C-Code Module in C++ Program - c++

My situation:
I have C code running on a microcontroller. To test this code I have written a test program in C++ that checks the C-functions. Since the test functions are very slow, I wanted to do the whole thing in parallel. However, I don't have much experience.
For example, I have a program module in C that looks like this:
/* c-code: */
static int a=0;
void set_a(int value){
a = value;
}
void inc_a(void){
a++;
}
int get_a(void){
return a;
}
Now I want to parallelize these functions in C++. However, I am bothered by the global variable a, which cannot be avoided in my situation.
In the QT environment I want to perform an "asynchronous run" of the function inc_a. This works but does not improve:
int foo(int somevalue){
set_a(somevalue);
inc_a();
return get_a();
}
int myinput = 1,myoutput;
QFuture<int> future = QtConcurrent::run(foo,myinput);
future.waitForFinished();
myoutput = future.result();
This is what I want:
int myinput1 = 1,myoutput1;
int myinput2 = 8,myoutput2;
QFuture<int> future1 = QtConcurrent::run(foo,myinput1);
QFuture<int> future2 = QtConcurrent::run(foo,myinput2);
future1.waitForFinished();
future2.waitForFinished();
myoutput1 = future1.result();
myoutput2 = future2.result();
So my first question is (to be sure): is it correct that the variable a (in C) is now the same in both threads? If not, I have to look over my code again.If yes, how do I solve the problem as elegantly as possible? I thought of creating two C-program modules with the same functionality. However, this makes the program very maintenance-unfriendly:
/* c-code: */
static int a1=0;
void set_a1(int value){
a1 = value;
}
void inc_a1(void){
a1++;
}
int get_a1(void){
return a1;
}
static int a2=0;
void set_a2(int value){
a2 = value;
}
void inc_a2(void){
a2++;
}
int get_a2(void){
return a2;
}
Is there a better way?

You are out of luck.
Ideally, rewrite your testable asset so that it carries round a state struct containing all those pesky globals, and maybe you will get away with it.
Vroomfondel also suggests that wrapping the offending C code in a namespace might hide the issue, if the code can be made to compile as C++.
You could create as many namespaces as you want parallel threads:
namespace TEST1
{
#include "offender.c"
}
namespace TEST2
{
#include "offender.c"
}
RetCode DoTest(int instance, TestId testid)
{
switch (instance)
{
case 1: return TEST1::DoTest(testid);
case 2: return TEST2::DoTest(testid);
}
return OUT_OF_RANGE;
}
If your target really uses global state and can't be changed, then you could consider using forks.
In a fork, a complete copy of the current state is made for the child to run in, and they both resume with just enough info so you know which is the child and which is the owner. You can also set up a pipe for them to communicate with each other. When a test completes, it transmits its status and exits its forked process.
Forks can be really good for test suites because each fork starts with a completely clean environment.
There is a /lot/ more to getting forking right than I think is reasonable to put as an answer to this question.
The third option is to drive the program externally, so that some monitor script or program launches multiple parallel instances that each run linearly through a subset of the test list. Ideally build in features so the monitor can dispatch tests on demand and load-balance.

Related

How do I assign an inline anonymous function to a C++ function pointer

I'm running a bit of code on an Arduino machine that uses C++. The setup (very roughly) is like below in the main function. The example is fairly contrived, and the code design doesn't really pass the smell check, but it's the best I can do.
There's a 3rd party WiFi library that I'm trying to add an "onDisconnect" hook to. When my TaskRunner class executes, I attach a function to this hook so that when the WiFi disconnects, my task runner is notified.
The challenge is I don't know the language well enough to assign an anonymous function that also keeps the "isWifiConnected in scope. I'm somewhat learning C++ on the fly so please feel free to change the title of the question as I might not even be asking the right one.
Note that I may not be looking for an anonymous function. I'm trying to update the isWifiConnected property when onDisconnect() is called.
#include <iostream>
using namespace std;
// A 3rd Party class
// https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnect.h
// https://github.com/Hieromon/AutoConnect/blob/master/src/AutoConnect.cpp
class AutoConnect {
public:
AutoConnect(){};
// I add this to the class as a hook to be called at about
// line 549 code block where AutoConnect calls disconnect code
void (*onDisconnect)();
};
void onDisconnect(){
cout << "Disconnecting!" << endl;
cout << "But how to make isWifiConnected false? I don't have access :/";
};
// My task runner
class TaskRunner {
public:
bool isWifiConnected;
AutoConnect *connector;
TaskRunner(AutoConnect & connector){
this->connector = & connector;
}
void execute(){
isWifiConnected = true;
// run some WiFi connection code ...
// assign the onDisconnect hook to be run when it disconnects
connector -> onDisconnect = onDisconnect;
// but how can I update "isWifiConnected" = false when the onDisconnect runs
// In JavaScript, I could do
/*
connector -> onDisconnect = function(){
// variable stays in scope due to closure.
isWifiConnected = false;
}
*/
}
};
int main() {
cout<<"Running ..." << endl;
// Instantiate my classes and inject WifiConnector into my task runner
AutoConnect connector;
TaskRunner runner = TaskRunner(connector);
// Simulate an event loop for effect
for(int i = 0; i < 10; i++){
if(i == 0) runner.execute();
// on some other condition, Wifi is disconnected
if(i == 9) connector.onDisconnect();
}
return 0;
}
Any ideas on how to update the TaskRunner's isWifiConnected variable? I've tried various pointer combinations but can't quite get it right.
Other issues with the code aside (see question comments):
You can store a lambda in a std::function:
Instead of void (*onDisconnect)(); declare it std::function<void()> onDisconnect;. (requires #include<functional>)
Then you can store a capturing lambda in it:
connector->onDisconnect = [this](){
isWifiConnected = false;
};
Since this stores a pointer to *this, you must make sure the the TaskRunner object outlives any potential call to this hook/lambda. Otherwise your program will have undefined behavior.
In particular currently the TaskRunner is declared after the AutoConnect object in main, meaning that the latter will be destroyed before the AutoConnect and therefore there will be a possibility of the lambda being called when TaskRunner has already been destroyed. This is particularly the case if AutoConnect's destructor may call the lambda. Whether it does or not I don't know.

How i can implement a BackgroundWorker in C++ STL

Hello i'm newbie in C++ specially on STL,
I need to create a function with an infinite loop to calculate and process big data (such as Genetic Algorithm), but i also need keep Ui responsive and update it within (after each round) that infinite loop and start/stop operation manually.
something like this:
bool working = false;
void do_process()
{
while(working)
{
// do some stuff
}
}
void btnStart()
{
working = true;
do_process();
}
void btnEnd()
{
working = false;
}
would you please guide me to a proper solution without any 3rdparty lib, thanks.
and apologies for terrible English.
The code below should get you started. But be careful, implementing a multi-threading application is generally a hard problem also for experienced users. Lot of knowledge is required about memory access synchronization and deadlock analysis. Consider the example below is really essential. For instance, in btnStart and btnStop you should check if a thread is already running. Checking the global bool working may require synchronization. Similarly, checking for null pointer may require synchronization. Bottom line, it is way more complicate than it may seem.
#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
#include <memory>
bool working = false;
std::unique_ptr<std::thread> t;
void do_process()
{
while(working)
{
std::cout << "Hi. I am a secondary thread and I am running.\n";
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void btnStart()
{
working = true;
t.reset(new std::thread(do_process)); // start the thread
}
void btnEnd()
{
working = false; // inform the thread of termination
t->join(); // wait for thread termination
t.reset(NULL);
}
int main()
{
std::cout << "Hi, I am the main thread.\n";
std::cout << "I'll now launch another thread and sleep for a while\n";
btnStart();
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
btnEnd();
std::cout << "What happened while I was slepping?\n";
return 0;
}
I am fairly new also to c++ but i have something that might help.
when i want to run something like an update to my code or to run something external without cramming my original project with code, i like to use ShellExecute to run another c++ program or external program. To use ShellExecute you need #include<windows.h>
For example if i want to update my program, i use #include<fstream>, #include<windows.h>, and #include<string> to check for a value in a file called 'updatereq.txt' (i make it my self). And in my program i run ifstream to check in the file if there is a '1'. If the if statement detects '1' it does this:
void Update(string filename)
{
ShellExecute(NULL,"open",filename.c_str(),NULL,NULL,SW_SHOWNORMAL)
}
This will run with:
HWND set as NULL, Operation set as: "open", File set as string:filenameconstant, Parameters set as NULL, Directory set as NULL(will run in the Directory of originally launching, usually at the main file), and Mode set as SW_SHOWNORMAL which will run it infront of you normally. This is also SW_SHOWMINIMIZED and SW_SHOWMAXIMIZED
Hope this helps!
PS: Remember to mention the file / program name that you are going to run when calling this function

Simultaneously running 2 or more boost testcases belonging to different test suites via cmd

Consider the following scenario:
BOOST_AUTO_TEST_SUITE(suite1)
{
BOOST_AUTO_TEST_CASE(case1)
{
//my test code here
}
}
BOOST_AUTO_TEST_SUITE(suite2)
{
BOOST_AUTO_TEST_CASE(case1)
{
//my test code here
}
BOOST_AUTO_TEST_CASE(case2)
{
//my test code here
}
}
Now, if I want to run suite1/case1 and suite2/case2 at once, I try the following command line argument:
MyProject.exe --run_test="suite1/case1, suite2/case2"
But this doesn't seem to run.
I know that I can separately run these test cases, as:
MyProject.exe --run_test="suite1/case1"
and
MyProject.exe --run_test="suite2/case2"
But I want to run them together at one go. What should I do?
Thanks in advance :)
This is not a feature currently supported by Boost.Test. The documentation states that you can use a comma separated list if the tests are in the same suite:
Running multiple test cases residing within the same test suite by listing their names in coma separated list.
You can also use wildcards to select suites and test cases, but depending on the names of your suites and cases, you may not be able to limit the selection to just to two cases you desire.
http://www.boost.org/doc/libs/1_55_0/libs/test/doc/html/utf/user-guide/runtime-config/run-by-name.html
Edit It seems I might have taken the question title a bit too literally. Running tests simultaneously means "in parallel" to me.
Anyways, if you are happy to run suite2/case1 as well, you can just
MyProject.exe --run_test="suite1,suite2"
See it Live On Coliru too.
Old answer: What is wrong with running the two processes in parallel? By all means, uncomplicate!
However, if you insist, you can fork copies of the main process:
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
static int relay_unit_test_main(std::vector<std::string> args);
int main()
{
if (int const child_pid = fork())
{
int exit_code = relay_unit_test_main({"--run_test=suite1"});
int child_status;
while (-1 == waitpid(child_pid, &child_status, 0));
if (!WIFEXITED(child_status)) {
std::cerr << "Child process (" << child_pid << ") failed" << std::endl;
return 1;
}
return exit_code? exit_code : WEXITSTATUS(child_status);
} else
{
return relay_unit_test_main({"--run_test=suite2"});
}
}
See it Live On Coliru
The function relay_unit_test_main is really nothing more than a convenience wrapper around unit_test_main that avoids meddling with argv[] manually:
static bool init_function() { return true; }
static int relay_unit_test_main(std::vector<std::string> args)
{
std::vector<char const*> c_args;
c_args.push_back("fake_program_name");
std::transform(args.begin(), args.end(), std::back_inserter(c_args), std::mem_fn(&std::string::data));
c_args.push_back(nullptr);
return unit_test_main( &init_function, c_args.size()-1, const_cast<char**>(c_args.data()) );
}
This actually spawns a child process - and even tries to usefully combine the exit code information. Having a separate process prevents the problems that you'd get from using code that wasn't designed for multi-threaded use on different threads.
One caveat remains: if your program does static initializations before entry of main(), and these use external resources (like, log files, e.g.) there might be conflicts. See
man fork(3)
Does Boost Log support process forking? for an example of a lib that has potential issues with fork()

How can I do automata/state machine coding in C++?

I have used it in another programming language and It's very usefull.
I cannot find anything about this for C++.
Let's for example take the following code:
void change();
enum
{
end = 0,
gmx
}
int
gExitType;
int main()
{
gExitType = end;
SetTimer(&change, 10000, 0);
return 0;
}
void ApplicationExit()
{
switch (gExitType)
{
case end:
printf("This application was ended by the server");
case gmx:
printf("This application was ended by the timer");
}
::exit(0);
}
void change()
{
gExitType = gmx;
ApplicationExit();
}
That's kind of how we would do it in C++, but when using state machine/automata I could do something like this in the other language:
void change();
int main()
{
state exitType:end;
SetTimer(&change, 10000, 0);
return 0;
}
void ApplicationExit() <exitType:end>
{
printf("This application was ended by the server");
}
void ApplicationExit() <exitType:gmx>
{
printf("This application ended by the timer");
}
void change()
{
state exitType:gmx;
ApplicationExit();
}
In my opition this is a really elegant way to achieve things.
How would I do this in C++? This code doesn't seem to work (obviously as I cannot find anything automata related to C++)
To clarify my opinion:
So what are the advantages to using this technique? Well, as you can clearly see the code is smaller; granted I added an enum to the first version to make the examples more similar but the ApplicationExit functions are definately smaller. It's also alot more explicit - you don't need large switch statements in functions to determine what's going on, if you wanted you could put the different ApplicationExits in different files to handle different sets of code independently. It also uses less global variables.
There are C++ libraries like Boost.statechart that specifically try to provide rich support for encoding state machines:
http://www.boost.org/doc/libs/1_54_0/libs/statechart/doc/tutorial.html
Besides this, one very elegant way to encode certain types of state machines is by defining them as a couroutine:
http://c2.com/cgi/wiki?CoRoutine
http://eli.thegreenplace.net/2009/08/29/co-routines-as-an-alternative-to-state-machines/
Coroutines are not directly supported in C++, but there are two possible approaches for
implementing them:
1) Using a technique similar to implementing a duff's device, explained in details here:
http://blog.think-async.com/search/label/coroutines
This is very similar to how C#'s iterators work for example and one limitation is that yielding form the coroutine can be done only from the topmost function in the coroutine call-stack. OTOH, the advantage of this method is that very little memory is required for each instance of the coroutine.
2) Allocating a separate stack and registers space for each coroutine.
This essentially makes the coroutine a full-blown thread of execution with the only difference that the user has full responsibility for the thread scheduling (also known as cooperative multi-tasking).
A portable implementation is available from boost:
http://www.boost.org/doc/libs/1_54_0/libs/coroutine/doc/html/coroutine/intro.html
For this particular example, you could use objects and polymorphism to represent the different states. For example:
class StateObject
{
public:
virtual void action(void) = 0;
};
class EndedBy : public StateObject
{
private:
const char *const reason;
public:
EndedBy( const char *const reason_ ) : reason( reason_ ) { }
virtual void action(void)
{
puts(reason);
}
};
EndedBy EndedByServer("This application was ended by the server");
EndedBy EndedByTimer ("This application ended by the timer");
StateObject *state = &EndedByServer;
void change()
{
state = &EndedByTimer;
}
void ApplicationExit()
{
state->action();
::exit(0);
}
int main()
{
SetTimer(&change, 10000, 0);
// whatever stuff here...
// presumably eventually causes ApplicationExit() to get called before return 0;
return 0;
}
That said, this isn't great design, and it isn't an FSM in the general sense. But, it would implement your immediate need.
You might look up the State Pattern (one reference: http://en.wikipedia.org/wiki/State_pattern ) for a more general treatment of this pattern.
The basic idea, though, is that each state is a subclass of some common "state" class, and you can use polymorphism to determine the different actions and behaviors represented by each state. A pointer to the common "state" base class then keeps track of the state you're currently in.
The state objects may be different types, or as in my example above, different instances of the same object configured differently, or a blend.
You can use Template value specialization over an int to achieve pretty much what you want.
(Sorry I'm at my tablet so I cannot provide an example, I will update on Sunday)

Avoding multiple thread spawns in pthreads

I have an application that is parallellized using pthreads. The application has a iterative routine call and a thread spawn within the rountine (pthread_create and pthread_join) to parallelize the computation intensive section in the routine. When I use an instrumenting tool like PIN to collect the statistics the tool reports statistics for several threads(no of threads x no of iterations). I beleive it is because it is spawning new set of threads each time the routine is called.
How can I ensure that I create the thread only once and all successive calls use the threads that have been created first.
When I do the same with OpenMP and then try to collect the statistics, I see that the threads are created only once. Is it beacause of the OpenMP runtime ?
EDIT:
im jus giving a simplified version of the code.
int main()
{
//some code
do {
compute_distance(objects,clusters, &delta); //routine with pthread
} while (delta > threshold )
}
void compute_distance(double **objects,double *clusters, double *delta)
{
//some code again
//computation moved to a separate parallel routine..
for (i=0, i<nthreads;i++)
pthread_create(&thread[i],&attr,parallel_compute_phase,(void*)&ip);
for (i=0, i<nthreads;i++)
rc = pthread_join(thread[i], &status);
}
I hope this clearly explains the problem.
How do we save the thread id and test if was already created?
You can make a simple thread pool implementation which creates threads and makes them sleep. Once a thread is required, instead of "pthread_create", you can ask the thread pool subsystem to pick up a thread and do the required work.. This will ensure your control over the number of threads..
An easy thing you can do with minimal code changes is to write some wrappers for pthread_create and _join. Basically you can do something like:
typedef struct {
volatile int go;
volatile int done;
pthread_t h;
void* (*fn)(void*);
void* args;
} pthread_w_t;
void* pthread_w_fn(void* args) {
pthread_w_t* p = (pthread_w_t*)args;
// just let the thread be killed at the end
for(;;) {
while (!p->go) { pthread_yield(); }; // yields are good
p->go = 0; // don't want to go again until told to
p->fn(p->args);
p->done = 1;
}
}
int pthread_create_w(pthread_w_t* th, pthread_attr_t* a,
void* (*fn)(void*), void* args) {
if (!th->h) {
th->done = 0;
th->go = 0;
th->fn = fn;
th->args = args;
pthread_create(&th->h,a,pthread_w_fn,th);
}
th->done = 0; //make sure join won't return too soon
th->go = 1; //and let the wrapper function start the real thread code
}
int pthread_join_w(pthread_w_t*th) {
while (!th->done) { pthread_yield(); };
}
and then you'll have to change your calls and pthread_ts, or create some #define macros to change pthread_create to pthread_create_w etc....and you'll have to init your pthread_w_ts to zero.
Messing with those volatiles can be troublesome though. you'll probably need to spend some time getting my rough outline to actually work properly.
To ensure something that several threads might try to do only happens once, use pthread_once(). To ensure something only happens once that might be done by a single thread, just use a bool (likely one in static storage).
Honestly, it would be far easier to answer your question for everyone if you would edit your question – not comment, since that destroys formatting – to contain the real code in question, including the OpenMP pragmas.