Author Topic: Parallel Processing Performance  (Read 3175 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Parallel Processing Performance
« on: June 29, 2012, 05:14:34 AM »
This little program test the performance of threads computing in Parallel. I have a four core CPU so I can run the main program plus three other threads full whack to saturate the CPU.

Each thread is set to computing the first 10000 prime numbers (excluding 1 and 2).

If The first thread is considered the main program, and I add threads to work independently on the same task. These are typical timings in seconds, for all to complete the task:

core1      +core2  +core3  +core4
1.6867    2.344    2.394    2.428

The code here is configured to run main + threadA. Just set the value of threads from 1 to 3.

Code: OxygenBasic
  1.  
  2. extern lib "kernel32.dll"
  3. '
  4. void   QueryPerformanceCounter(quad*c)
  5. void   QueryPerformanceFrequency(quad*freq)
  6. sys    CreateThread (sys lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, *lpThreadId)
  7. dword  WaitForMultipleObjects(sys nCount,*lpHandles, bWaitAll, dwMilliseconds)
  8. bool   CloseHandle(sys hObject)
  9. void   Sleep(sys dwMilliSeconds)
  10. '
  11. quad freq,t1,t2
  12. QueryPerformanceFrequency freq
  13. QueryPerformanceCounter   t1
  14. '
  15. % max     10000
  16.  
  17. double p1[max]
  18. double p2[max]
  19. double p3[max]
  20. double p4[max]
  21.  
  22.  
  23. macro FindPrimes(p)
  24. finit
  25. double n=1
  26. sys c,k
  27. do
  28.   n+=2
  29.   if c>=max then exit do
  30.   '
  31.  'IS IT DIVISIBLE BE ANY PREVIOUS PRIME
  32.  '
  33.  for k=1 to c
  34.      if frac(n/p[k])=0 then exit for
  35.   next
  36.   '
  37.  if k>c then
  38.     c++
  39.     p[c]=n
  40.   end if
  41. end do
  42. end macro
  43.  
  44.  
  45. function threadA(sys v) as sys
  46. FindPrimes(p1)
  47. end function
  48.  
  49. function threadB(sys v) as sys
  50. FindPrimes(p2)
  51. end function
  52.  
  53. function threadC(sys v) as sys
  54. FindPrimes(p3)
  55. end function
  56.  
  57. end extern
  58.  
  59. function main(sys v)
  60. FindPrimes(p4)
  61. end function
  62.  
  63.  
  64. % threads 1
  65.  
  66. % INFINITE 0xFFFFFFFF  'Infinite timeout
  67.  
  68. sys Funs[threads]={@threadA,@threadB,@threadC}
  69. sys  hThread[threads], id[threads], i
  70. '
  71. '
  72. for i=1 to threads
  73.   hThread[ i ] =  CreateThread 0,0,funs[ i ],i,0,id[ i ]
  74. next
  75.  
  76. main 1
  77.  
  78. if threads>0 then
  79.   WaitForMultipleObjects Threads, hThread, 1, INFINITE
  80. end if
  81.  
  82. for i=1 to Threads
  83.   CloseHandle hThread[ i ]
  84. next
  85.  
  86. QueryPerformanceCounter t2
  87. print str((t2-t1)/freq,3) " secs    " max "    " p1[max] 'last prime

Charles
« Last Edit: June 29, 2012, 05:20:36 AM by Charles Pegge »

Charles Pegge

  • Guest
Re: Parallel Processing Performance
« Reply #1 on: June 30, 2012, 04:33:43 AM »
This task is to search for a number (in a set of random numbers)  with the highest prime factor. The numbers and primes are first generated then each core is set to work to find the highest prime factor for a portion of the numbers. When all the cores are finished, the results are checked to find the number with the highest factor.

http://rosettacode.org/wiki/Parallel_calculations#OxygenBasic

Charles


Mike Lobanovsky

  • Guest
Re: Parallel Processing Performance
« Reply #2 on: November 30, 2014, 12:09:32 PM »
Oh my,

Somebody send this script to that poor soul at Basicprogramming dot org now! He's going to pay $19.99 at some murky site to check his calc of the first 400 prime numbers! ;D


(Sorry for being a bit necrophylic tho)

edcronos

  • Guest
Re: Parallel Processing Performance
« Reply #3 on: December 22, 2017, 01:33:24 AM »
I do not know if I understood correctly, but does Oxygen allow parallel processing?
a question, is it possible to know the state of a nucleus to select the most idle, or select a nucleus for a variable to be controlled by the end of the functions?
I do not know if it is possible, but let's suppose that a routine is running, in case this routine is running in a kernel, if it has loops, error checks and interrupt processes, in the middle of the loops are called various functions, select which core will the fuse use so it will not overload what is already being used?

thank you

Charles Pegge

  • Guest
Re: Parallel Processing Performance
« Reply #4 on: December 22, 2017, 08:11:01 AM »
There is no formal syntax for parallel processing yet. But multithreading through the Windows API will often allocate threads to different CPU cores, if they are available.

For large scale number crunching with 32 bit values, OpenCL could provide the infrastructure for using graphics card hardware, with potentially hundreds of cores spinning in parallel.