update this code to run parallel on 2 processor

This is a discussion on update this code to run parallel on 2 processor within the C++ Programming forums, part of the General Programming Boards category; by calling thread i want 500 of the array on cpu and the other 500 elements of array on another ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    update this code to run parallel on 2 processor

    by calling thread
    i want 500 of the array on cpu and the other 500 elements of array on another cpu parallel
    this is the code to find prime numbers in array of 1000 elements

    Code:
    #include <iostream> 
    using namespace std; 
    int main () 
    { 
        int prime, range=1000; 
        int n; 
        for ( prime = 2; prime<=range; prime++ ) 
        { 
            for ( n=2; n < prime; n++ ) 
      
      
                if (prime%n==0 ) 
                    break; 
                if ( n==prime ) 
                    cout << prime << endl; 
                } 
        return 0; 
    }
    please help me
    Last edited by aboazoz; 05-16-2010 at 03:12 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Do you know anything about threads? If you do post some code. You can look up pthreads or another library.
    You can also look at OpenMP if you want to use it. It is perfect for this kind of algorithms (will do the job in a few lines), but it is better to learn how to use threads if you don't know so.

  3. #3
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    OK I'll help: "Hey Thread!"

    Sorry, with such an ambiguous request I could not resist. If you are determined not to learn about threads but still want to do parallel processing development (which I would not suggest; if you try to do stuff that runs on multiple threads w/o learning them at a low-level is asking for pain) then I think your only option is something like TBB Home

    Seriously, turning serialized code into smoothly operating and well-designed multicore code is stuff I tend to charge a lot of money to do because it is definitely something I would label as non-trivial..

    If you don't want to pay someone and Thread Building Blocks is out of the question then learning something like PThreads is your only option. I would also suggest learning about IPC, mutex, condition variables and semaphores. Of course MK will pop up in a minute suggesting fork() but what you do with your time is up to you...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    This request is ridiculous. It's akin to asking for a second person to help you clean your long concrete driveway ... EACH WITH AN INFANT TOOTHBRUSH!

    In other words, more of something is not the answer when you're doing it wrong to begin with!

    I could literally change two characters in that code to get about a 2x speedup, and could make many more changes that could each also provide a very significant improvement. Then again, starting out with a better algorithm can be many times faster again. Multi-threading is by no means a silver bullet.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    3
    my question is clear
    i mean running program on multiprocessor
    Last edited by aboazoz; 05-18-2010 at 05:57 AM.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    My answer is also clear, that is not an effective way to speed up that program.
    In fact it could make it slower, given that setting up a second thread might take similar amounts of time to finding primes from a range of 500.
    What is the purpose here?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array
    By mouse666666 in forum C Programming
    Replies: 36
    Last Post: 04-11-2010, 02:27 AM
  2. code wont run plz help structs
    By real138 in forum C++ Programming
    Replies: 12
    Last Post: 02-22-2010, 01:17 PM
  3. Update code for Vista 64
    By mdoland in forum C++ Programming
    Replies: 13
    Last Post: 06-02-2008, 04:34 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. code to run (linux) programs WITH arguments
    By N8760 in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2001, 02:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21