Help with this code to run on multiprocesses

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Help with this code to run on multiprocesses

#1 Post by einstein1969 » 22 Nov 2013 14:42

Hi,

I have a code that need to run on multicore (multiprocess).

I do not have much experience with multiprocess on dos batch. Can anyone help me rewrite code?

In the meantime, I am following this Multi-process Advanced Encryption Standard (AES) thread could be a similar solution.

The code as written in play without being checked. It can be seen for educational purposes and it's only one piece of a big project.

In addition I also have another problem. I do not have a multiprocessor computer, so I am looking for someone who can also do the tests.

Thank you guys.

Einstein1969
Last edited by einstein1969 on 24 Nov 2013 12:46, edited 2 times in total.

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Help with this code to run on multiprocesses

#2 Post by einstein1969 » 22 Nov 2013 15:19

The code:

Code: Select all

@echo off & setlocal EnableDelayedExpansion & if not "%1"=="" goto %1

mode con cols=90 lines=31 & rem  mode con rate=31 delay=0

start "" /B cmd /c "%~f0" input_3D

for /F "delims==" %%f in ('set') do if /i not "%%f"=="tmp" set "%%f="

set "P=Û.²±°\-."
set /a n=0, r=-4, s=24, q=1, c=3, d=1

set "cod1=))*2)/100) + n*10000/64 )/10000 ^ ((2048000000/(4096-(%%_*128+%%#"
set "cod2=))*2)/100)/100)-((2048000000/(4096-(%%_*128+%%#"
set "cod3=))) %% 2000000)/10000*(2048000000/(4096-(%%_*128+%%#"
set "cod4=))*2)/1000000) )&q)"
set "cod5=(( ( ((((15625*(4096-(%%_*128+%%#"

for /L %%n in (0,1,10000) do (
  set t0=!time!
  type nul>%tmp%\3d.dat.txt
  For /L %%_ in (!s!,-1,-!r!) do (
    set B=
    For /L %%# in (21,4,108) do (

   set /a "a=10000 + %cod5%%cod3%%cod2%%cod1%%cod4%*1000 + %cod5%+1%cod3%+1%cod2%+1%cod1%+1%cod4%*100 + %cod5%+2%cod3%+2%cod2%+2%cod1%+2%cod4%*10 +%cod5%+3%cod3%+3%cod2%+3%cod1%+3%cod4%"

   for /f "tokens=1-4" %%i in ("!a:~1,1! !a:~2,1! !a:~3,1! !a:~4,1!") do set B=!B!!P:~%%~i,1!!P:~%%~j,1!!P:~%%~k,1!!P:~%%~l,1!
    )
    echo( !B!>>%tmp%\3d.dat.txt
  )
 
  set t1=!time!

  call :manage_input
  if /I "!input_char!"=="q" cls & echo Quitting... & goto :eof

  cls&type %tmp%\3d.dat.txt

  for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!t1: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000, FPS=1000/a"&echo(!t0!-!t1!=!a! FPS=!FPS:~0,-1!.!FPS:~-1! SPACE for toggle max speed.&set a=&set t1=
  echo(Use keys: A D W S for direction/move, Z X for pattern, C V for step. Q for quit.
  echo(DEBUG: %%n Input:!input_char! n:!n! r:!r! s:!s! q:!q! c:!c!
  if !d! equ 1 call :wait
  if not defined cod ( for /L %%b in (1,1,5) do (set "cod%%b=") & set cod=1 )
)

:manage_input
(
   set lc=!input_char!

     2>nul (<"%tmp%\in.dat" set /p "input_get=")
     2>nul set "input_char=!input_get!"

   if /I "!input_char!"=="a" set /a n=n-c
   if /I "!input_char!"=="d" set /a n=n+c
   if /I "!input_char!"=="w" if !s! gtr 0 set /a r=r+1,s=s-1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"=="s" if !s! lss 26 set /a r=r-1,s=s+1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"=="z" if !q! gtr 1 set /a q=q-1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"=="x" if !q! lss 6 set /a q=q+1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"=="v" if !c! lss 6 set /a c=c+1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"=="c" if !c! gtr 1 set /a c=c-1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"=="c" if !c! gtr 1 set /a c=c-1&>"%tmp%\in.dat" echo(!lc!
   if /I "!input_char!"==" " set /a d=-d&>"%tmp%\in.dat" echo(!lc!

goto :eof
)

:input_3D
  >"%tmp%\in.dat" echo(a
:input_3D_loop
( set "Key="
  for /f "delims=" %%A in ('xcopy /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%A"
  set "key=!key:~-1!"
  >"%tmp%\in.dat" echo(!key!
  if /I Not "!key!" == "q" goto :input_3D_loop
)
goto :eof

:wait
  c:\Windows\System32\PING.EXE 192.0.2.0 -n 1 -w 500 >nul
goto :eof


EDIT: Modified code for legibility

Einstein1969

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with this code to run on multiprocesses

#3 Post by Aacini » 23 Nov 2013 02:01

I want to copy here a part of my first post in the topic you linked:

Aacini wrote:Excuse me. If the objective is to generate a more efficient Batch code for this application, I certainly will not do that trying to understand (parts of) your code and then modifying it... I prefer to consult the original specification you used to develop this application and, after review and understand it, propose modifications.

An example of this point is the generation of Galois constants. The original code had problems caused by the large amount of individual SET commands required for this task, so the first attempts to optimize it was oriented to modify the way to execute the numerous SET commands. When I asked Magialisk for the method he used to calculate the constants I expected a reference or a description of the method, but his answer was a link to his code and this note: "the algorithm can be sped up greatly if someone can help me "break" the FOR loop early", so the next optimization attempts tried to solve this point. Magialisk's answer included this phrase: "Since you were asking how I generated the constants though, you'll find the algorithm I used in that thread so you can re-write it for your own use". If you review the method I finally used to generate Galois constants you realize that there is no way to extract that method from Magialisk's code. On the other hand, if he would answer me at that moment with his Wikipedia reference on "Peasant's algorithm", I am pretty sure that the job of understand the method would not been as hard as it was! :evil:

------------------------------------------------------------------------

I would like to review a description of this problem in order to evaluate it and see if it is suitable to be solved via a multi-process solution. If it is, and the problem is interesting, I gladly try to propose an appropriate solution. However, at this moment there are multiple questions on this problem: involves it a fast-and-slow processing parts over a file, so it may be solved in a similar way of the previously linked problem? Have it another scheme suitable for a multi-process solution? The only point clear right now is that I will not try to clear these doubts reviewing a Batch program code!

Antonio

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Help with this code to run on multiprocesses

#4 Post by einstein1969 » 23 Nov 2013 06:10

@Aacini

I did not understand what you need.

However, the code uses a file even if initially used environment variables with no file!

I have developed various implementations and this is the one that seems more powerful on uniprocessor. But I have to redo the tests.

If you need a reference from which to start, this is what I found most suitable to explain the path I have followed.
Although the link I saw for the first time today!

what-is-the-math-behind-this-ray-like-animation

Was this what you were looking for?

EDIT: I have modified the subject for those who need to know what it is.

Einstein1969

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with this code to run on multiprocesses

#5 Post by Aacini » 23 Nov 2013 10:29

einstein1969 wrote:@Aacini

I did not understand what you need.

Einstein1969


Suppose for a moment that you have NOT your Batch code, but you want to solve this problem and ask for help. What do you do? Well, you should state your problem in a clear way: first describe it, then add a couple examples and the expected output. If the problem is complex perhaps you need to add a couple references with additional information. The clearer you describe your problem the better solutions you will get. The major cause of problems in proposed solutions is that the programmer didn't understood the problem, specially in the details. Non-computer literated people frequently think that a programmer must know about all the possible problems that may be solved via a program, but this knowledge is not easy to get even if the programmer review a program that solve the problem already !

If you may get help from a Raytracing expert that know a little programming or from an expert programmer that know nothing about raytracing, what do you prefer: teach programming to the former or teach raytracing to the latter?

Antonio

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Help with this code to run on multiprocesses

#6 Post by einstein1969 » 23 Nov 2013 15:23

Aacini wrote:
einstein1969 wrote:@Aacini

I did not understand what you need.

Einstein1969


Suppose for a moment that you have NOT your Batch code, but you want to solve this problem and ask for help. What do you do? Well, you should state your problem in a clear way: first describe it, then add a couple examples and the expected output. If the problem is complex perhaps you need to add a couple references with additional information. The clearer you describe your problem the better solutions you will get. The major cause of problems in proposed solutions is that the programmer didn't understood the problem, specially in the details. Non-computer literated people frequently think that a programmer must know about all the possible problems that may be solved via a program, but this knowledge is not easy to get even if the programmer review a program that solve the problem already !

If you may get help from a Raytracing expert that know a little programming or from an expert programmer that know nothing about raytracing, what do you prefer: teach programming to the former or teach raytracing to the latter?

Antonio



Hi Antonio , I still do not understand.

I try to make an example closer to you. (I hope it is simple)

Suppose we want to draw a screen function and we rotate over time. Using a function that you know.

This is the script that I have not tested thoroughly and I have written voluntarily with non-optimized code.

How can parallelize this script?

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set /a cols=34, lines=38

mode con cols=%cols% lines=%lines%

set /a lines-=4, nchar=cols*lines, angle=0

set t0=%time%

For /L %%. in (1,1,100) do (

  cls

  rem rotate two degree at second
  for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!time: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000, angle=(a*20000/100)%%450000"

  call :INT_SinD_CosD !angle! sin cos

  set /a "cos=cos/100000, sin=sin/100000" & rem echo t=!a! angle=!angle! sin=!sin! cos=!cos!

  for /L %%y in (1,1,%lines%) do (

   for /L %%x in (1,1,%cols%) do (

      set /a "x=(%%x-cols/2)*100/8, y=(%%y-lines/2)*100/8"

      rem rotate x,y
      set /a "x1=x*cos/100-y*sin/100, y=x*sin/100+y*cos/100, x=x1"
      
      set /a "c1=(x*x/100+y*y/100), c2=c1*c1/100*c1/100*c1/100*c1/100, c3=((x*x/100-y*y/100)*8*x/100*y/100), c4=c3*c3/100, c=(c2-c4)"

      if !c! lss 0 (<nul set /p "=*") else <nul set /p "=."
   )
  )
)

goto :eof

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:INT_SinD_CosD  Angle  SinResultVAR  CosResultVAR
::
::      -- Return the sine and cosine for the specified angle in 100*micro-degrees.
::         ie. 10000 is one degree.
::
::         Call :INT_sinD_CosD Angle sin cos
::            
::      -- Angle    [in]   in 100 micro-Degree is an integer in [-449999,449999]
::
::      -- SinResultVAR    [out]   var reference to return value of sine [-100000000,100000000]
::
::      -- CosResultVAR    [out]   var reference to return value of cosine [-100000000,100000000]
::
::
::  Version history
::
:: 0.1   16/9/2013   
::
::         First version Beta! Internal 27bit used
::
::         From Francesco Poscetti aka Einstein1969
::
::         NOTE: This Function allow you to calculate almost all other common
::         trigonometric function. Use the CORDIC modified algorithm.
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SetLocal EnableDelayedExpansion

   set /a "a=%~1"

   if %a% lss -449999 echo Not implemented. Exit. & goto :eof
   if %a% gtr  449999 echo Not implemented. Exit. & goto :eof

   set /a size=-1
   For %%a in (450000000 265650512 140362435 71250163 35763344 17899106
   8951737 4476142 2238105 1119057 559529 279765 139882 69941 34971 17485
   8743 4371 2186 1093 546 273 137 68 34 17 9 4 2 1) do (set /a "size+=1" & set "at!size!=%%a")

   if %a% geq 0 (     set /a x=607252935, angle=!a!*10*10*10, angle-=!at0!, y=x
               ) else set /a x=607252935, angle=!a!*10*10*10, angle+=!at0!, y=-x

   For /L %%# in (1,1,!size!) do (
     rem echo [%1] %%# angle:!angle! x:!x! y:!y! at%%#=!at%%#! size:!size!
     if !angle! lss 0        (set /a "angle+=!at%%#!, dx=x/(1<<%%#), x+=y/(1<<%%#), y-=dx"
     ) else if !angle! gtr 0 (set /a "angle-=!at%%#!, dx=x/(1<<%%#), x-=y/(1<<%%#), y+=dx")
   )

endlocal & set /a %2=(%y%+55)/100, %3=(%x%+55)/100
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: Raytracing-Raycasting multiprocess

#7 Post by Magialisk » 23 Nov 2013 17:27

einstein perhaps I can help, since I'm the one that got Antonio all spun up in that other thread :D

Anyone who is going to attempt to help you is going to need to understand your problem. Example: What is raytracing? Do you have a good source of information about what you're doing and why? What sources did you use to develop the code, or is this all just in your head? You want to get into multiprocessing, but to do that we'd need to understand how the problem can be broken up into small pieces and parallellized. AES was easy as every 128 bits is it's own independent problem to be solved quickly and go to the next 128 bits. Not all problems can even be solved this way.

The idea is that for someone who knows what they're doing, it is much more productive, and will give a better result, if they can go understand your problem and design a good solution from scratch. Looking at the code you wrote, the best they can do is guess at why you would write it that way and make minor improvements.

Antonio has been a huge help to me in the other thread, we've bounced many ideas off each other and I've incorporated a few of his improvements into my original code. He solved the problem his own way from scratch after I explained what I was doing and why. Seeing his version gave me additional ideas of how to make my own better. At least from my perspective it's been a great process.

I'd be happy to take a look at what you're doing for you if you can post a little more information. I do basically know what raytracing is, but I dont undersatnd it's application in batch? Do you intend to draw vectorized graphics or something?

einstein1969
Expert
Posts: 960
Joined: 15 Jun 2012 13:16
Location: Italy, Rome

Re: Raytracing-Raycasting multiprocess

#8 Post by einstein1969 » 24 Nov 2013 14:03

Magialisk wrote:einstein perhaps I can help, since I'm the one that got Antonio all spun up in that other thread :D

Anyone who is going to attempt to help you is going to need to understand your problem. Example: What is raytracing? Do you have a good source of information about what you're doing and why? What sources did you use to develop the code, or is this all just in your head? You want to get into multiprocessing, but to do that we'd need to understand how the problem can be broken up into small pieces and parallellized. AES was easy as every 128 bits is it's own independent problem to be solved quickly and go to the next 128 bits. Not all problems can even be solved this way.

The idea is that for someone who knows what they're doing, it is much more productive, and will give a better result, if they can go understand your problem and design a good solution from scratch. Looking at the code you wrote, the best they can do is guess at why you would write it that way and make minor improvements.

Antonio has been a huge help to me in the other thread, we've bounced many ideas off each other and I've incorporated a few of his improvements into my original code. He solved the problem his own way from scratch after I explained what I was doing and why. Seeing his version gave me additional ideas of how to make my own better. At least from my perspective it's been a great process.

I'd be happy to take a look at what you're doing for you if you can post a little more information. I do basically know what raytracing is, but I dont undersatnd it's application in batch? Do you intend to draw vectorized graphics or something?


Hi Magialisk,

I have posted e simple script that not use raytracing. But have the same form. Animations in batch. (working with files and directories boring ) we can start from here

but I dont undersatnd it's application in batch?


Magialisk wrote:Thanks Dave for the kind word. I think I've said too many times already that this was never meant as anything more than a proof of concept, so practicality is a distant secondary concern :D . This was a somewhat natural conclusion from that "string encoding" thread up through the Vigenere cipher, the Enigma machine and now ultimately AES. I just like to see what can be done in batch, it's fun for me.

Idem...
You can see it as a game. But the purpose is to know in depth the operation of the dos batch.

Example: What is raytracing? Do you have a good source of information about what you're doing and why? What sources did you use to develop the code, or is this all just in your head?

I start like this what-is-the-math-behind-this-ray-like-animation and have implemented in dos batch for deepen the dos batch.

How to parallelized? The expert of batch dos know the limit of dos. And they know the most common solutions with their limitations and possibilities.

The algos can be paralelized in many ways. you need to find the most efficient in dos batch.

I honestly do not know where to start because I do not know right now. I just created a project (the mini-monitor) that uses multiprocessing and communication between them but I never investigated.

Edit: I edited the subject because i have changed a little the problem in "draw a animated equation on screen"

This problem has many solutions i things :?

Einstein1969

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with this code to run on multiprocesses

#9 Post by Aacini » 24 Nov 2013 19:07

Ok, here we go...

If you want that your script run on multi-processes you may use the same method explained in my answer to Magialisk's topic and placed below "The Batch code below is an example of this method; it may be used as general format for any application that follows this scheme"; indeed, I used it as a guideline to convert your program. However, in this case there are not input records to process, just output records represented by the lines in the screen. I slightly modified the method so each parallel process run on its own set of screen lines interleaved by the same number of processes, that is, if there are 3 processes the first one generate lines 1, 4, 7, etc. until line 34; the second one the lines 2, 5, 8..33; and 3,6,9..32 for the last one.

Code: Select all

@echo off & setlocal EnableDelayedExpansion


rem Multi-process dispatcher:
set "param=%~1"
if "!param:~0,1!" equ ":" (
   shift
   goto !param:~1!
)

REM set /A N=NUMBER_OF_PROCESSORS-1
set N=3


set /a cols=34, lines=38

mode con cols=%cols% lines=%lines%

set /a lines-=4, nchar=cols*lines, angle=0


rem Values used in parallel processes
set /A linesPerProc=lines/N, missingLines=lines%%N, Nminus1=N-1


set t0=%time%

For /L %%. in (1,1,100) do (

   cls

   rem rotate two degree at second
   for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!time: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000, angle=(a*20000/100)%%450000"

   call :INT_SinD_CosD !angle! sin cos

   set /a "cos=cos/100000, sin=sin/100000" & rem echo t=!a! angle=!angle! sin=!sin! cos=!cos!

   rem Set the initial output turn
   del processActive.* 2>NUL
   del outputTurn.* 2>NUL
   echo X >outputTurn.0

   rem Start the parallel processes
   set /A startLine=1, missing=missingLines, carry=1
   for /L %%p in (0,1,!Nminus1!) do (
      if !missing! equ 0 set carry=0
      set /A "endLine=startLine+N*(linesPerProc-1+carry)"
      echo X > processActive.%%p
      start "" /B "%~F0" :ShowInterleavedLines %%p !startLine! %N% !endLine!
      set /A startLine+=1, missing-=1
   )

   rem Wait for parallel processes to end
   call :waitForActiveProcesses

)

goto :eof


:waitForActiveProcesses
if exist processActive.* goto waitForActiveProcesses
exit /B


:ShowInterleavedLines turn startLine interleave endLine

for /L %%y in (%2,%3,%4) do (
   set "line="

   for /L %%x in (1,1,%cols%) do (

      set /a "x=(%%x-cols/2)*100/8, y=(%%y-lines/2)*100/8"

      rem rotate x,y
      set /a "x1=x*cos/100-y*sin/100, y=x*sin/100+y*cos/100, x=x1"
     
      set /a "c1=(x*x/100+y*y/100), c2=c1*c1/100*c1/100*c1/100*c1/100, c3=((x*x/100-y*y/100)*8*x/100*y/100), c4=c3*c3/100, c=(c2-c4)"

      if !c! lss 0 (set "line=!line!*") else set "line=!line!."
   )

   if not exist outputTurn.%1 call :waitMyTurn %1

   set /P "=!line!" < nul

   set /A "nextTurn=(%1+1) %% N"
   ren outputTurn.%1 outputTurn.!nextTurn!

)

del processActive.%1
exit


:waitMyTurn
if not exist outputTurn.%1 goto waitMyTurn
exit /B


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:INT_SinD_CosD  Angle  SinResultVAR  CosResultVAR
::
::      -- Return the sine and cosine for the specified angle in 100*micro-degrees.
::         ie. 10000 is one degree.
::
::         Call :INT_sinD_CosD Angle sin cos
::           
::      -- Angle    [in]   in 100 micro-Degree is an integer in [-449999,449999]
::
::      -- SinResultVAR    [out]   var reference to return value of sine [-100000000,100000000]
::
::      -- CosResultVAR    [out]   var reference to return value of cosine [-100000000,100000000]
::
::
::  Version history
::
:: 0.1   16/9/2013   
::
::         First version Beta! Internal 27bit used
::
::         From Francesco Poscetti aka Einstein1969
::
::         NOTE: This Function allow you to calculate almost all other common
::         trigonometric function. Use the CORDIC modified algorithm.
::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SetLocal EnableDelayedExpansion

   set /a "a=%~1"

   if %a% lss -449999 echo Not implemented. Exit. & goto :eof
   if %a% gtr  449999 echo Not implemented. Exit. & goto :eof

   set /a size=-1
   For %%a in (450000000 265650512 140362435 71250163 35763344 17899106
   8951737 4476142 2238105 1119057 559529 279765 139882 69941 34971 17485
   8743 4371 2186 1093 546 273 137 68 34 17 9 4 2 1) do (set /a "size+=1" & set "at!size!=%%a")

   if %a% geq 0 (     set /a x=607252935, angle=!a!*10*10*10, angle-=!at0!, y=x
               ) else set /a x=607252935, angle=!a!*10*10*10, angle+=!at0!, y=-x

   For /L %%# in (1,1,!size!) do (
     rem echo [%1] %%# angle:!angle! x:!x! y:!y! at%%#=!at%%#! size:!size!
     if !angle! lss 0        (set /a "angle+=!at%%#!, dx=x/(1<<%%#), x+=y/(1<<%%#), y-=dx"
     ) else if !angle! gtr 0 (set /a "angle-=!at%%#!, dx=x/(1<<%%#), x-=y/(1<<%%#), y+=dx")
   )

endlocal & set /a %2=(%y%+55)/100, %3=(%x%+55)/100
goto :eof
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


On the other hand, if you would want that your script run faster, then there are several modifications that may help to do that. For example, the simple modification required in the multi-process method of store the output characters in a line and then display the line with one SET /P command, instead of show every character via individual SET /P commands, cause that the program run 21% faster. Another simple modification, although it run just 16% faster, allows to display the result of the whole screen in a single operation, so in this case the waiting time between screen frames doesn't matter to much.

I am pretty sure that a slightly larger modification in the method to calculate Sin and Cos values will result in additional time gains in the single process mode; of course, this modification may be combined with the multi-process one. However, I was confused by the values that your INT_SinD_CosD routine use.



In my previous posts I tried to explain you that having a better knowledge about a problem will conduce to better solutions, and asked you for additional information; however, it seems that you are not interested in such better solutions and just wanted to "parallelize your script". I have presented my solution here that allows your script to run on multi-processes, as you requested... I would have liked to complete and show you the other modifications that allows to decrease the execution time, but those modifications have absolutely no relation with the multi-process method and I have spent too much time in this matter!

Antonio

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: Help with this code to run on multiprocesses

#10 Post by Magialisk » 25 Nov 2013 01:51

That sweeping ray animation you linked to is pretty neat. I see now what you're trying to do, and I downloaded and ran both your code and Antonio's.

I do agree with Antonio, if you want to parallelize this the right way would be interleaving lines. If you think of it like a GPU framebuffer, the child processes would add lines to the framebuffer, and once the buffer was full it would dump to the screen as one frame.

Just for the heck of it I ran both versions of the code and roughly (via clock and eyeballs) timed the execution. Antonio's uses about 93% of my quad core and makes about 2.5 frames per second. einstein's uses about 30% of my CPU and makes about 0.85 fps. So 3x faster performance and 3x more CPU use, makes sense on the surface... Maybe I'll get a chance to try it at work tomorrow on the 12-thread to see how it scales.

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with this code to run on multiprocesses

#11 Post by Aacini » 25 Nov 2013 17:44

Magialisk wrote:I do agree with Antonio, if you want to parallelize this the right way would be interleaving lines. If you think of it like a GPU framebuffer, the child processes would add lines to the framebuffer, and once the buffer was full it would dump to the screen as one frame.


Yes, but in the case of multi-process solution the output must be displayed in individual lines, unless that a file be used to store the complete frame and then show it in one operation, but this point will add even more overhead to the method.

In the single-process method it is very easy to show complete frames, as shown below. Although this method is slower than the multi-process one, the animation is much more pleasant to see.

Code: Select all

@echo off & setlocal EnableDelayedExpansion

set /a cols=34, lines=38

mode con cols=%cols% lines=%lines%

set /a lines-=4, nchar=cols*lines, angle=0

set t0=%time%

For /L %%. in (1,1,100) do (

   set "line="

   rem rotate two degree at second
   for /F "tokens=1-8 delims=:.," %%a in ("!t0: =0!:!time: =0!") do set /a "a=(((1%%e-1%%a)*60)+1%%f-1%%b)*6000+1%%g%%h-1%%c%%d, a+=(a>>31) & 8640000, angle=(a*20000/100)%%450000"

   call :INT_SinD_CosD !angle! sin cos

   set /a "cos=cos/100000, sin=sin/100000" & rem echo t=!a! angle=!angle! sin=!sin! cos=!cos!

   for /L %%y in (1,1,%lines%) do (

      for /L %%x in (1,1,%cols%) do (

         set /a "x=(%%x-cols/2)*100/8, y=(%%y-lines/2)*100/8"

         rem rotate x,y
         set /a "x1=x*cos/100-y*sin/100, y=x*sin/100+y*cos/100, x=x1"
     
         set /a "c1=(x*x/100+y*y/100), c2=c1*c1/100*c1/100*c1/100*c1/100, c3=((x*x/100-y*y/100)*8*x/100*y/100), c4=c3*c3/100, c=(c2-c4)"

         if !c! lss 0 (set "line=!line!*") else set "line=!line!."
      )

   )

   cls

   set /P "=!line!" < nul

)

goto :EOF


Antonio
Last edited by Aacini on 25 Nov 2013 21:30, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with this code to run on multiprocesses

#12 Post by foxidrive » 25 Nov 2013 21:01

Antonio, I haven't been following the thread closely but decided to try what you've posted. Is it missing a label or a routine?

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Help with this code to run on multiprocesses

#13 Post by Aacini » 25 Nov 2013 21:31

Yes, this routine:

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:INT_SinD_CosD  Angle  SinResultVAR  CosResultVAR
::

Copy it from anyone of previous posts.

Note that in original post I missed a goto :EOF (fixed...)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Help with this code to run on multiprocesses

#14 Post by foxidrive » 25 Nov 2013 21:59

It works well, and is interesting to see in batch.

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: Help with this code to run on multiprocesses

#15 Post by Magialisk » 25 Nov 2013 23:52

Antonio, maybe it's just personal preference but I much prefer your second version that does the "framebuffer" display technique instead of the original line by line output version.

Granted the line by line version runs faster overall, but the image it produces is not as appealing (to me). When I mentioned the framebuffer in my original post I too thought first of using a file as an intermediate storage, which as you say would be slow, so I didn't explicitly suggest that. Then I had a really crazy idea but I didn't elaborate on that either...

What if you used an array structure and made multiple processes to fill in the values. This could use the line-by-line method, building up the "Screen" framebuffer a line at a time like I alluded to in my post. The tricky part would be returning each line of values over the boundary when each child process returns? Anyway, if you could indicate when the framebuffer was full (every 38th line?) you would then display it, combining the best of both approaches without needing a slow intermediate file?

I don't have the time to try this out myself right now, but maybe off the top of your head you know of an intrinsic flaw to that approach? Where do you find the time to take on everyone else's pet projects anyway? :D

Post Reply