Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Multicore coding
New Posts  All Forums:Forum Nav:

[C++] Multicore coding - Page 2

post #11 of 45
Which libraries are you including?

If you don't already know , I think (I haven't done much of this programming) the approach you need to take for this problem is to create multiple threads (based off the number of cores available), and then set the affinity of each thread to a different cpu core, and have this all done inside your program. If you only want to multitask, setting the affinity won't be necessary, and you can just run multiple threads on one core. That won't be true multitasking, but time-sharing, and it will give you the same effect.

I'll type up an example from my book (in C), it should compile (I don't currently have C compiler installed [maybe I should just do that...]). It won't deal with affinity, but it will give you some code with threads to tinker with.
Edited by hoth17 - 12/26/10 at 5:11pm
post #12 of 45
Thread Starter 
Well, I've got a function, and I've got to have it run on as many worker threads as possible.

So I need to find out how many threads I've got to work with, and then run that function on each extra thread.

I've got a C++ book here, but it doesn't cover threads (which is a shame, since it's served me well for everything else). And the code that our lecturer has given us doesn't actually run, so I'm assuming I'm missing some libraries there too...

This was the code she gave, for reference:

Code:

SYSTEM_INFO si;
GetSystemInfo(&si);
int ncpu = si.dwNumberOfProcessors; 
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #13 of 45
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <wait.h>

int
main(void)
{
int filedes[2]; //array of file descriptors for the pipe

pid_t fpid; //forked process id
int status; //forked process status

//create the interprocessing communications pipe
if (pipe (filedes) < 0)
printf ("Error Creating The Interprocess Communications Pipe\
");

//Create a new process
fpid = fork();

//Both the parent and new processes continue running here
if (fpid < 0)
{
//The new process was not created
printf ("Error Creating The New Process\
");

//Close the read and write file descriptors of the pipe
if (close (filedes[0]) != 0)
printf ("Error Closing File Descriptors 0\
");
if (close (filedes[1]) != 0)
printf ("Error Closing File Descriptor 1\
");
}

else if (fpid == 0)
{
//This is the new process
print f ("Forked Process ID %d\
", getpid());

//Close the write file descriptor of the pipe
if (close (filedes[1] != 0)
printf ("Error Closing File Descriptor 0\
");

//Duplicate & assign read file descriptor to standard input
if (dup2 (filedes[0], STDIN_FILEENO) != STDIN_FILENO)
printf ("Error Closing Duplicating File Descriptor 0\
");

//Replace this process image with the process image from the executable file child.exe
if (execl("child.exe", "child.exe", NULL) < 0)
printf ("Error Replacing This Process Image\
");

}

else

{
//This is the parent process
printf ("parent process ID %d\
", getpid());

//Close the read file descriptor of the pipe
if (close (filedes[0] != 0)
printf ("Error Closing Pipe\
");

//Wait for the forked process to begin executing
sleep(1);

printf ("Parent Process Writing '%s'\
", MESSAGE);

//Write the message to the write file descriptor and flush
//the write file descriptor by writing a newline character
if (write (filedes[0], "\
", 1) < 0)
printf ("Error Writing to file descriptor 1\
");
if (write (filedes[1], "\
", 1) < 0)
printf ("Error writing to file descriptor 1\
");

printf ("Parent process waiting for forked process\
");

//wait for the forked process to complete & display status
if (wait (&status) == fpid)
printf ("forked process status %d\
", WEXITSTAT:US(status));
else
printf ("Error waiting for the forked process\
");

printf("Parent process resuming\
");
}

printf ("Parent process stopping\
");

return (0);
}
hopefully that will compile and let you mess with it.
post #14 of 45
Thread Starter 
Nope; loads of undeclared errors.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #15 of 45
will you post the errors?
post #16 of 45
Thread Starter 
4:18: wait.h: No such file or directory

function `int main()':
14: error: `pipe' undeclared (first use this function)
14: error: (Each undeclared identifier is reported only once for each function it appears in.)
18: error: `fork' undeclared (first use this function)
36: error: `print' undeclared (first use this function)
36: error: expected `;' before "f"
40: error: expected `)' before "printf"
43: error: `STDIN_FILEENO' undeclared (first use this function)
60: error: expected `)' before "printf"
63: error: `sleep' undeclared (first use this function)
65: error: `MESSAGE' undeclared (first use this function)
77: error: `wait' undeclared (first use this function)
78: error: `WEXITSTAT' undeclared (first use this function)
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #17 of 45
Okay, missed one of your posts. The code I gave you isn't exactly what you are trying to do. That uses multiple processes your going to use threads. They are similar but a little different.
post #18 of 45
Thread Starter 
I've looked at a load of links online, and I just can't get my head around it all.

I need someone to slowly (and kindly, if possible) walk me through it.

On a lighter note, I got the above code sorted (the one to find the number of cores available). Needed the windows.h library.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #19 of 45
Here, this one made a lot of sense and gives you sample code.

http://xoax.net/comp/cpp/misc/CreateThread.php
post #20 of 45
Thread Starter 
I got somewhere!

Here's the code I've got at the moment:

Code:

#include "windows.h"
#include <iostream>
#include <iomanip>
using namespace std;
DWORD WINAPI ThreadFn(LPVOID vpParam);
int main ()
{
    unsigned int uiCounter = 0;
    DWORD qThreadID;
    HANDLE Thread_1 = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);
    
    
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    int ncpu = si.dwNumberOfProcessors;
    cout << "Number of available cores: " << ncpu << endl << endl;
    
    char cChar = ' ';
    while (cChar != 'q')
    {
      if (uiCounter < 4294967295)
         cout << "Thread " << qThreadID << ": " << uiCounter << endl;
      else
      {
        cout << "Counter ended." << endl;
        break;
      }
           
      cChar = (char)getchar();
    }
    
    CloseHandle(Thread_1);
    
    system("PAUSE");
    return 0;
}
DWORD WINAPI ThreadFn(LPVOID vpParam)
{
      unsigned int& uirCounter = *((unsigned int*)vpParam);
      int t = clock();
      DWORD qThreadID;
      
      while (uirCounter < 0xFFFFFFFF)
      {
        uirCounter++;
      }
      
      cout << "Time taken: " << fixed << setprecision(3)
           << (double) (clock() - t) / CLOCKS_PER_SEC << " seconds."
           << endl;
      
      return 0;

A couple of questions now:

1) "DWORD qThreadID" is always a different number every time I run the program. How do I get it to show the which thread it is?

2) How do I add more threads? I tried adding:
HANDLE Thread_2 = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);
HANDLE Thread_3 = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);
HANDLE Thread_4 = CreateThread(0, 0, ThreadFn, &uiCounter, 0, &qThreadID);

But it was still the same thread doing all the work (since qThreadID and the runtime was always the same.

In short, how do I get the program to use all 4 threads, and cout the thread # too?

Progress though.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Multicore coding