Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented helper to use multiple kernels
created extern for pythons multiprocessing.Process class
- Loading branch information
Showing
4 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package duell.helpers; | ||
|
||
import haxe.Constraints.Function; | ||
|
||
import python.Tuple; | ||
import python.multiprocessing.Process; | ||
|
||
import duell.objects.DuellProcess; | ||
import duell.helpers.PlatformHelper; | ||
import duell.helpers.LogHelper; | ||
|
||
using StringTools; | ||
|
||
typedef ParallelJob = { | ||
@:optional var group:Int; | ||
var job:Function; | ||
@:optional var name:String; | ||
@:optional var args:Array<Dynamic>; | ||
} | ||
|
||
class ParallelProcessHelper { | ||
|
||
private var processList:Array<Process>; | ||
|
||
public function new() {} | ||
|
||
public function run( jobs:Array<ParallelJob> ):Void { | ||
if( jobs == null ) return; | ||
|
||
var numberKernels = getLogicalKernels(); | ||
processList = new Array<Process>(); | ||
LogHelper.info('', 'Running jobs on $numberKernels Kernels ... '); | ||
for( i in 0...jobs.length ) { | ||
var job = jobs[ i ]; | ||
var args = Reflect.hasField( job, 'args' ) ? new Tuple( job.args ) : null; | ||
var p = new Process( Reflect.field( job, 'group' ), | ||
job.job, | ||
Reflect.field( job, 'name'), | ||
args); | ||
processList.push( p ); | ||
p.start(); | ||
|
||
if( numberKernels == processList.length || i == jobs.length-1 ) { | ||
for ( proc in processList ) proc.join(); | ||
|
||
processList = []; | ||
} | ||
} | ||
} | ||
|
||
private function getLogicalKernels():Int { | ||
var numberKernelsString = ""; | ||
var numberKernels = 0; | ||
|
||
if (PlatformHelper.hostPlatform == Platform.WINDOWS){ | ||
var dp = new DuellProcess("", "wmlc", ["cpu", "get", "NumberOfLogicalProcessors"], {block:true, systemCommand:true, errorMessage: "grabbing number of kernels"}); | ||
var dpOutput = dp.getCompleteStdout().toString(); | ||
var rows = dpOutput.split("\n"); | ||
numberKernelsString = rows.length > 1 ? rows[1].trim() : ""; | ||
} else { | ||
var dp = new DuellProcess("", "sysctl", ["-n", "hw.logicalcpu_max"], {block:true, systemCommand:true, errorMessage: "grabbing number of kernels"}); | ||
numberKernelsString = dp.getCompleteStdout().toString(); | ||
} | ||
|
||
try { | ||
numberKernels = Std.parseInt( numberKernelsString ); | ||
} catch( e:Dynamic ) { | ||
LogHelper.warn("Error grabbing number of kernels! " + e); | ||
} | ||
|
||
return numberKernels != 0 && numberKernels != null ? numberKernels : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright (c) 2003-2015, GameDuell GmbH | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY | ||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
* LOSS OF USE,a DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package python.multiprocessing; | ||
|
||
import python.Tuple; | ||
|
||
import haxe.Constraints.Function; | ||
|
||
@:pythonImport("multiprocessing", "Process") | ||
extern class Process { | ||
|
||
var name:String; | ||
var pid:Int; | ||
var exitcode:Int; | ||
|
||
function new<T>(group:Null<Int>=null, target:Function=null, name:String=null, ?args:Tuple<Dynamic> ); | ||
function run():Void; | ||
function start():Void; | ||
function join( ?timeout:Int ):Void; | ||
function is_alive():Bool; | ||
function terminate():Void; | ||
} |