C++ multithread / multi cpu problem


I have a class in c++ (ansi standard) that is to be run on a cray !

My problem is that i want to run 24 threads each thread on a different cpu, with each thread calling the c++ class with slightly different parameters.

I would appreciate it if anyone out there could point me in the right direction as to how to code this or even better if anyone has any sample source that could help

regards
Psueds
psuedsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexVirochovskyCommented:
There many examples of multithread
Apps. I can send you examples from
Petzold Book,and no metter hou many
threads you use.But if you want
examples with cray specific, i am out.
0
jkrCommented:
Do you mean you want the methods to be reentrant?
0
captainkirkCommented:
I remember that when I was writing FORTRAN for the CDC Cyber 175's I had a nominal bit of experience with the Cray 1, which was similar to the Cyber in some respects - the Cray being a parallel processing machine, the FORTRAN compilers were just being enhanced to be able to take advantage of the multiprocessor architecture. I would suspect that the C++ compiler design would have been similarly enhanced... I will poke around a bit and see if I can come up with any specifics for you...
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

jhanceCommented:
It seems to me that the CRAY compiler does processor allocation and scheduling for you, doesn't it?  The data dependency rules on the CRAY parallel architecture are much too complex to try and follow manually.

I really don't think you have a choice in the matter.
0
KangaRooCommented:
If the question is, does C++ provide support for multithreading/multiprocessor, the answer is no.
0
captainkirkCommented:
Perhaps not plain ANSI standard C/C++, but the Cray's native compiler may in fact provide just such support...
0
kejinCommented:
what you need is simply 24 different objects
all created from the same class. Pass the
objects as "arg" of the thread creating
function. So, each of your thread get
a different object even all of them are in
same class.

pseudo code example:

class A {
   int _param;
  public:
    A(int p) : _param(p) {}
    void run();
    static void startup(void* arg);
};

void A::startup(void* arg)
{
    ((A*)arg)->run();
}

main() {
    A a1(1), a2(2), ...., a24(24);
    pthread_create(..., A::startup, &a1);
    ....
    pthread_create(..., A:startup, &a24);
    ....
}
0
captainkirkCommented:
That's what I'm thinking should be the case...
0
abancroftCommented:
What about processor affinity for each thread?
What about shared data protection?
0
captainkirkCommented:
If the compiler has been enhanced as in the FORTRAN case, then each thread will be assigned a different processor. Shared memory should be protected using the standard multithreading schemes, I would think, since there is only one central core to the machine, and it really makes no difference whether there is one processor or several. The threads should be designed to cooperate as usual....
0
abancroftCommented:
Is this a shared memory machine?

I suppose that the Cray compiler extensions may hide this sort of implementation detail.
0
captainkirkCommented:
Guess what?? I found a copy of the Cray Supercomputing API set documents for a T90 under UNICOS -- perhaps this might come in handy (maybe for other flavors of the machine)... give me your email and I can send the .PDF file to ya...
0
IexpertCommented:
The tricky part of this is to get the object context
for each thread. This is because the thread API needs
a stack frame for the thread function which is incompatable
with that generated for a C++ class member function (because
of the implicit passing of the object context (the this pointer)).

The following code shows how to get around this problem.
Note the code is specific to WIN32, but will work on the
Cray using the same technique, just changing the thread
API function calls. Do you use pthreads on the Cray ?

Anyway any questions just contact me @ Padraig@Brady001.iol.ie
----------------------------
class MyClass
{
public:
      MyClass();
      HANDLE hThread;
      DWORD dwThreadId;
      
private:
      void ThreadFunction(void);
      
      /*
       * static member function means a global function,
       * IE no this pointer is passed to it on the stack.
       */
      static DWORD WINAPI ThreadStarter(LPVOID lpParams);
};

/*
 * Note even though this is a member function of SASmediationDevice,
 * it's a static member and hence no "this" pointer is implicitly passed to it.
 * Therefore I have to explicitly pass the "this" pointer so I can
 * call the DataReceiveThread member function for the appropriate object.
 *
 * Note the reason this is required is that when windoze creates the thread
 * it will call a specified function. There obviously must be an agreement
 * between this function and windows on the format of the stack frame,
 * and a class member function doesn't fit the bill.
 */
DWORD WINAPI MyClass::ThreadStarter(LPVOID lpParams)
{
      MyClass* pMyObject;

      //this pointer passed as param
      pMyObject = *(MyClass**)lpParams;

      /*
       * Note since calling implicitly through "this" pointer,
       * the DataReceiveThread can even be a virtual function.
       */
      pMyObject->ThreadFunction();

      return 0;
}

MyClass::MyClass()
{
      //Pass "this" pointer to Thread, so it can call the
      //member function correctly.
      dwThrdParam = (long) this;

      /*
       * Note not using CreateThread() (which doesn't need the 2
       * typedefs below), since some runtime lib functions
       * are called from the started threads, and this isn't
       * safe for threads started with CreateThread().
       *
       * Note also that pthread_create() would be equivalent
       * call to use in POSIX threads. see following for more info:
       * http://pauillac.inria.fr/~xleroy/linuxthreads/
       */
      typedef unsigned (WINAPI* PBEGINTHREADEX_THREADFUNC) (LPVOID lpParams);
      typedef unsigned* PBEGINTHREADEX_THREAD;

      hThread = (HANDLE) _beginthreadex(
            NULL,                                    // no security attributes
            0,                                    // use default stack size
            (PBEGINTHREADEX_THREADFUNC)ThreadStarter,      // thread function
            &dwThrdParam,                              // argument to thread function
            0,                                    // use default creation flags
            (PBEGINTHREADEX_THREAD)&dwThreadId);            // returns the thread identifier (system unique ID)

      // Check the return value for success.
      if (hThread == NULL)
            printf("Error: Creation of Thread failed");
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TryCommented:
CalTech has written a superset of the C++ compiler to handle parallel procesing for the Sun MicroSystem machines, using Solaris.  Maybe something at their website might offer you assistance on the design of such applications.

There is a book by Ian Foster, titled, "Designing and Building Parallel Programs" that has its entire content published on the internet.  I have the book and I checked the website and found the entire content of the book at this address, "http://www.mcs.anl.gov/dbpp".

The book uses a superset of C++ called, CC++ (or Compositional C++), but it's strictly for applications written for parallel processing.  Perhaps you might find somethings on it, useful.
0
TryCommented:
I have several multiprocessor machines, and I run NT 4.0 on all of them.

NT is a multithreaded application and I design and program other multithreaded applications using C++ to run on these machines, under NT.  Therefore what is happening there is a multi-processor machine, using a multithreaded operating system, running a set of multithreaded applications and that is somewhat near to what you are trying to do.

Of course, in the design and programming of any multithreaded application, much extra care is needed to keep the whole situation flowing smoothly, but I don't go about doing it the way you have described in your question.  I pretty much leave it up to the operating system to decide what part of the program gets processed by which processor.  I simply coordinate the overall flow to make certain that the threads are all operating in harmony with each other.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.