MultiThread a for loop

On 16/04/2013 at 07:16, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   13/14 
Platform:   Windows  ;   
Language(s) :     C++  ;

---------
Hi,

currently I'm doing a Plugin which is doing an intensive calculation, one part of this calculation is copy a large amount of memory to another memory location with a for loop

something like:

for (int lc = 0;lc < 100000000; lc++){

> NewData[9*lc] = OldData[lc].s[0];
> NewData[9*lc+1] = OldData[lc].s[1];
> NewData[9*lc+2] = OldData[lc].s[2];
>
>
> NewData[9*lc+3] = OldData_A[lc].s[0];
> NewData[9*lc+4] = OldData_A[lc].s[1];
> NewData[9*lc+5] = OldData_A[lc].s[2];
>
>
> NewData[9*lc+6] = OldData_B[lc].s[0];
> NewData[9*lc+7] = OldData_B[lc].s[1];
> NewData[9*lc+8] = OldData_B[lc].s[2];
}
>
>

I have seen MenuTest.cpp but didn't understand how to do it with a simple for loop
in simple cases it is just like this

#pragma omp parallel for num_threads(6)
and then my for loop

but found that c4d sdk got problems with OpenMP ...

thanks in advance
Mohamed Sakr

On 16/04/2013 at 07:45, xxxxxxxx wrote:

I have a C++ example of using threading with a while loop on my plugins site: https://sites.google.com/site/scottayersmedia/plugins

-ScottA

On 16/04/2013 at 08:22, xxxxxxxx wrote:

You can feed a couple of threads (usually as many as CPUs are available) with packages. They
all need to know what part of the iteration they need to work on. For example, thread 1 works
from 0 ... 1000, thread 2 from 1001 to 2000, etc. Make sure you are using locks/semaphores
wherever it is necessary so you will ever enter a critical section (eg. unsynchronized memory
writes/reads) pseudo C++ code:

  
  
class WorkerThread : public C4DThread {
  
    PackageManager* manager;
  
public:
  
    WorkerThread(PackageManager* manager);
  
    // C4DThread Overrides
  
    void Main();
  
};
  
WorkerThread::WorkerThread(PackageManager* manager) : manager(manager) {
}
  
WorkerThread::Main() {
    LONG* oldmem = NULL;
    LONG* newmem = NULL;
    LONG count;
    while (!TestBreak() && manager->Fetch(&oldmem, &newmem, &count)) {
        for (LONG i=0; i < count; i++) {
            newmen[i] = oldmem[i];
        }
    }
}

Best,
-Niklas

On 16/04/2013 at 08:50, xxxxxxxx wrote:

@ Niklas how to use the WorkerThread class in my loop
what i understand is that WorkerThread::Main() is doing a copy of LONG Array to another LONG Array
how to use this in parallel?

assume I'm a complete noob 😄

On 16/04/2013 at 09:52, xxxxxxxx wrote:

The LONG was just a placeholder for the datatype you are using, you need to transfer the example
to apply it to your use case. You can start multiple threads (allocate an array of WorkerThreads)
that all recieve the same PackageManager instance (needs to be implemented by you). When
a WorkerThread has nothing to do it asks the PackageManager for a new package (the old
memory location, the new memory location and the number of elements) that it can work with
until the PackageManager tells it that there is nothing actually left to be done.

-Niklas