[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

C++ multithread / multi cpu problem

Posted on 1999-11-24
15
Medium Priority
?
313 Views
Last Modified: 2012-05-04

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
0
Comment
Question by:psueds
  • 5
  • 2
  • 2
  • +6
15 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2231727
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
 
LVL 86

Expert Comment

by:jkr
ID: 2231733
Do you mean you want the methods to be reentrant?
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2231839
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 32

Expert Comment

by:jhance
ID: 2231851
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
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2231976
If the question is, does C++ provide support for multithreading/multiprocessor, the answer is no.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2231996
Perhaps not plain ANSI standard C/C++, but the Cray's native compiler may in fact provide just such support...
0
 
LVL 2

Expert Comment

by:kejin
ID: 2232221
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
 
LVL 4

Expert Comment

by:captainkirk
ID: 2232234
That's what I'm thinking should be the case...
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2232235
What about processor affinity for each thread?
What about shared data protection?
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2232261
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
 
LVL 4

Expert Comment

by:abancroft
ID: 2232284
Is this a shared memory machine?

I suppose that the Cray compiler extensions may hide this sort of implementation detail.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2232293
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
 
LVL 3

Accepted Solution

by:
Iexpert earned 2000 total points
ID: 2234169
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
 
LVL 3

Expert Comment

by:Try
ID: 2235840
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
 
LVL 3

Expert Comment

by:Try
ID: 2235862
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question