0

Given any set of functions (or basic blocks) of a c code, I need to divide them into two (imagine having two processors) so that it could run parallel and could improve runtime performance?

Any idea on how to achieve it? I already have memory access dependencies in between those given set of functions.

Macow tdy
  • 59
  • 6
  • Have you considered threads, such as those declared the standard `` header? – Eric Postpischil Jul 27 '20 at 16:43
  • Thanks. Yes I have looked in to threads. Here the problem is I have specific set of functions (methods) and I need to divide them in to two processors considering their memory dependencies. After all to increase runtime performance – Macow tdy Jul 27 '20 at 16:51
  • So create two threads and execute the functions you want on each thread. – Eric Postpischil Jul 27 '20 at 16:53

1 Answers1

0

You might want to use a thread and set its affinity.

Since you do not mention what OS you're on, if you're using Windows you'll need to use the SetThreadAffinityMask function. If you're running on a POSIX based system (like Linux/Unix and even macOS to some extent), you can use the pthread_setaffinity_np function, but you must take care with that function since the _np means "non-portable" and might not work on your target system.

For both of these, they require a native thread handle, which in Windows is one created with CreateThread or _beginthread/_beginthreadex (i.e. a HANDLE), for POSIX systems it's threads created with pthread_create (i.e. a pthread_t).

If you're using the C11 <threads.h> header and created a thrd_t with thrd_create, then you can pass that as the thread handle to the set-affinity functions, though you might have to cast it to the specific type (e.g. (HANDLE)tid or (pthread_t)tid).

If you switch to C++11 and use the std::thread type, you will have to pass the value returned from the native_handle call into the set-affinity functions.

This Q&A has some examples on how to use the pthread_setaffinity_np, and this one has some info regarding SetThreadAffinityMask.

As an aside, without seeing code, setting the affinity might not get you want you want regarding parallel performance, you'll need to run some tests on your code to verify that it actually does increase the performance of your code, versus writing tighter, more CPU-cache friendly code.

I hope that can help.

txtechhelp
  • 6,625
  • 1
  • 30
  • 39