@@ -43,83 +43,39 @@ var GPU = (function() {
4343 if ( paramObj === undefined ) {
4444 paramObj = { } ;
4545 }
46-
46+
47+ //
48+ // Replace the kernel function and param object
49+ //
50+ this . _kernelFunction = kernel ;
51+ this . _kernelParamObj = paramObj || this . _kernelParamObj || { } ;
52+
4753 //
4854 // Get the config, fallbacks to default value if not set
4955 //
50- paramObj . dimensions = paramObj . dimensions || [ ] ;
5156 var mode = paramObj . mode && paramObj . mode . toLowerCase ( ) ;
5257 this . computeMode = mode || "auto" ;
5358
54- if ( mode == "cpu" ) {
55- return this . _mode_cpu ( kernel , paramObj ) ;
56- }
57-
5859 //
59- // Attempts to do the glsl conversion
60+ // Get the Synchronous executor
6061 //
61- try {
62- return this . _mode_gpu ( kernel , paramObj ) ;
63- } catch ( e ) {
64- if ( mode != "gpu" ) {
65- console . warning ( "Falling back to CPU!" ) ;
66- this . computeMode = mode = "cpu" ;
67- return this . _mode_cpu ( kernel , paramObj ) ;
68- } else {
69- throw e ;
70- }
71- }
62+ var ret = this . getSynchronousModeExecutor ( ) ;
63+ // Allow class refence from function
64+ ret . gpujs = this ;
65+ // Execute callback
66+ ret . exec = ret . execute = GPUUtils . functionBinder ( this . execute , this ) ;
67+
68+ // The Synchronous kernel
69+ this . _kernelSynchronousExecutor = ret ; //For exec to reference
70+
71+ return ret ;
7272 } ;
7373 GPU . prototype . createKernel = createKernel ;
7474
75- ///
76- /// Function: setKernel
77- ///
78- /// Set the kernel function and its configuration settings
79- ///
80- /// This is the ASYNC alternative to createKernel, when used in conjuncture to executeKernel
81- ///
82- /// |---------------|---------------|---------------------------------------------------------------------------|
83- /// | Name | Default value | Description |
84- /// |---------------|---------------|---------------------------------------------------------------------------|
85- /// | dimensions | [1024] | Thread dimension array |
86- /// | mode | null | CPU / GPU configuration mode, "auto" / null. Has the following modes. |
87- /// | | | + null / "auto" : Attempts to build GPU mode, else fallbacks |
88- /// | | | + "gpu" : Attempts to build GPU mode, else fallbacks |
89- /// | | | + "cpu" : Forces JS fallback mode only |
90- /// |---------------|---------------|---------------------------------------------------------------------------|
91- ///
92- /// Parameters:
93- /// inputFunction {JS Function} The calling input function to perform the conversion
94- /// paramObj {Object} [Optional] The parameter configuration object (see above),
95- /// uses previously set param object or blank object if not specified
96- ///
97- /// Returns:
98- /// {GPU} returns itself
99- ///
100- function setKernel ( kernel , paramObj ) {
101- //
102- // basic parameters safety checks
103- //
104- if ( kernel === undefined ) {
105- throw "Missing kernel parameter" ;
106- }
107- if ( ! GPUUtils . isFunction ( kernel ) ) {
108- throw "kernel parameter not a function" ;
109- }
110-
111- //
112- // Replace the kernel function and param object
113- //
114- this . _kernelFunction = kernel ;
115- this . _kernelParamObj = paramObj || this . _kernelParamObj || { } ;
116- }
117- GPU . prototype . setKernel = setKernel ;
118-
11975 ///
12076 /// Function: getKernelFunction
12177 ///
122- /// Get and returns the kernel function previously set by `setKernel `
78+ /// Get and returns the kernel function previously set by `createKernel `
12379 ///
12480 /// Returns:
12581 /// {JS Function} The calling input function
@@ -132,7 +88,7 @@ var GPU = (function() {
13288 ///
13389 /// Function: getKernelParamObj
13490 ///
135- /// Get and returns the kernel parameter object previously set by `setKernel `
91+ /// Get and returns the kernel parameter object previously set by `createKernel `
13692 ///
13793 /// Returns:
13894 /// {JS Function} The calling input function
@@ -153,64 +109,19 @@ var GPU = (function() {
153109 /// Returns:
154110 /// {Promise} returns the promise object for the result / failure
155111 ///
156- function executeKernel ( ) {
112+ function execute ( ) {
157113 //
158- // Get the arguments
159- //
160- var args = ( arguments . length === 1 ? [ arguments [ 0 ] ] : Array . apply ( null , arguments ) ) ;
161-
162- //
163114 // Prepare the required objects
164115 //
165- var kernel = this . _kernelFunction ;
166- var paramObj = this . _kernelParamObj ;
116+ var args = ( arguments . length === 1 ? [ arguments [ 0 ] ] : Array . apply ( null , arguments ) ) ;
167117 var self = this ;
168118
169119 //
170- // Get the config, fallbacks to default value if not set
171- //
172- paramObj . dimensions = paramObj . dimensions || [ ] ;
173- var mode = paramObj . mode && paramObj . mode . toLowerCase ( ) ;
174- self . computeMode = mode = mode || "auto" ;
175-
176- //
177- // Setup and return the promise, and execute the function
120+ // Setup and return the promise, and execute the function, in synchronous mode
178121 //
179122 return GPUUtils . newPromise ( function ( accept , reject ) {
180123 try {
181- //
182- // Does computation in CPU mode
183- //
184- if ( mode == "cpu" ) {
185- self . computeMode = "cpu" ;
186- accept ( self . _mode_cpu ( kernel , paramObj ) . apply ( self , args ) ) ;
187- return ;
188- }
189-
190- //
191- // Attempts to do computation in GPU mode
192- //
193- try {
194- self . computeMode = "gpu" ;
195- accept ( self . _mode_gpu ( kernel , paramObj ) . apply ( self , args ) ) ;
196- return ;
197- } catch ( e ) {
198- if ( mode != "gpu" ) {
199- //
200- // CPU fallback after GPU failure
201- //
202- console . warn ( "Falling back to CPU!" ) ;
203- self . computeMode = "cpu" ;
204- accept ( self . _mode_cpu ( kernel , paramObj ) . apply ( self , args ) ) ;
205- return ;
206- } else {
207- //
208- // Error : throw rejection
209- //
210- reject ( e ) ;
211- return ;
212- }
213- }
124+ accept ( self . _kernelSynchronousExecutor . apply ( self , args ) ) ;
214125 } catch ( e ) {
215126 //
216127 // Error : throw rejection
@@ -220,7 +131,8 @@ var GPU = (function() {
220131 }
221132 } ) ;
222133 }
223- GPU . prototype . executeKernel = executeKernel ;
134+ GPU . prototype . execute = execute ;
135+ GPU . prototype . exec = execute ;
224136
225137 ///
226138 /// Function: addFunction
0 commit comments