Max 5 API Reference
00001 00002 00003 // this class may be used to split image into n pieces and call functions of the form 00004 //void jit_op_calculate_ndim(void *data, long dimcount, long *dim, long planecount, t_jit_matrix_info *in1_minfo, char *bip1, 00005 // t_jit_matrix_info *in2_minfo, char *bip2, t_jit_matrix_info *out_minfo, char *bop/*, opttional t_jit_parallel_ndim_worker *w */); 00006 00007 #include "jit.common.h" 00008 #include "ext_obex.h" 00009 #include "ext_systhread.h" 00010 #include "parallel.h" 00011 00012 typedef struct _long_splat 00013 { 00014 void *a[32]; 00015 } t_long_splat; 00016 00017 typedef void (*t_long_splat_fn)(t_long_splat); 00018 00019 /** 00020 * @defgroup parallelutilmod Parallel Utility Module 00021 @ingroup jitter 00022 */ 00023 00024 void jit_parallel_ndim_single(t_jit_parallel_ndim *p); 00025 void jit_parallel_ndim_multi(t_parallel_worker *w); 00026 00027 //this is not reentrant 00028 t_parallel_task *_jit_parallel_task=NULL; 00029 static long _jit_parallel_init=0; 00030 long _jit_parallel=0; 00031 long _jit_parallelthreads=0; 00032 long _jit_parallelthresh=10000; // minimum cell count to use parallel processing 00033 long _jit_paralleldebug=0; 00034 00035 void jit_parallel_utils_init(void) 00036 { 00037 if (!_jit_parallel_init) { 00038 parallel_init(); 00039 _jit_parallel_init = 1; 00040 _jit_parallelthreads = parallel_processorcount(); 00041 if (_jit_parallelthreads>1) 00042 _jit_parallel = 1; 00043 //jit_object_post((t_object *)x,"jitter parallel=%d, threadcount=%d",_jit_parallel,_jit_parallelthreads); 00044 } 00045 } 00046 00047 /** 00048 * Tasks N-dimensional matrix calcuations to multiple threads if appropriate. 00049 * This function is ultimately what the other parallel 00050 * utility functions call after having set up the 00051 * t_jit_parallel_ndim struct. The operation is tasked 00052 * to multiple threads if all of the following conditions 00053 * are met: 00054 * - multiple processors or cores are present 00055 * - parallel processing is enabled 00056 * - the size of the matrix data is larger then the parallel threshold 00057 * 00058 * @ingroup parallelutilmod 00059 * 00060 * @param p parallel ndim calc data 00061 * 00062 */ 00063 void jit_parallel_ndim_calc(t_jit_parallel_ndim *p) 00064 { 00065 long i; 00066 unsigned long itemcount=1; 00067 00068 for (i=0;i<p->dimcount;i++) { 00069 itemcount *= p->dim[i]; 00070 } 00071 if ((!_jit_parallel)||((long)itemcount<_jit_parallelthresh)) 00072 jit_parallel_ndim_single(p); 00073 else { 00074 if (!_jit_parallel_task) { 00075 _jit_parallel_task = parallel_task_new(p,(method)jit_parallel_ndim_multi,_jit_parallelthreads); 00076 } 00077 parallel_task_data(_jit_parallel_task,p); 00078 parallel_task_workerproc(_jit_parallel_task,(method)jit_parallel_ndim_multi); //will need later for different kinds 00079 parallel_task_execute(_jit_parallel_task); 00080 } 00081 } 00082 00083 void jit_parallel_ndim_single(t_jit_parallel_ndim *p) 00084 { 00085 long i,j; 00086 t_long_splat splat; 00087 t_jit_parallel_ndim_worker pw; 00088 00089 pw.paralleldata = p; 00090 pw.workercount = 1; 00091 pw.workerid = 0; 00092 pw.offset[0] = 0; 00093 pw.offset[1] = 0; 00094 00095 splat.a[0] = (void *)p->data; 00096 splat.a[1] = (void *)p->dimcount; 00097 splat.a[2] = (void *)p->dim; 00098 splat.a[3] = (void *)p->planecount; 00099 for (i=0,j=4;i<p->iocount;i++) 00100 { 00101 splat.a[j++] = (void *)p->io[i].minfo; 00102 splat.a[j++] = (void *)p->io[i].bp; 00103 } 00104 splat.a[j++] = (void *)&pw; 00105 (*(t_long_splat_fn)p->fn)(splat); 00106 if (_jit_paralleldebug) 00107 post("parallel single"); 00108 } 00109 00110 void jit_parallel_ndim_multi(t_parallel_worker *w) 00111 { 00112 long i,j,offset,dim[32]; 00113 t_long_splat splat; 00114 t_jit_parallel_ndim *p=(t_jit_parallel_ndim *)w->data; 00115 t_jit_matrix_info *minfo[JIT_PARALLEL_NDIM_MAX_IO]; 00116 t_jit_parallel_ndim_worker pw; 00117 char *bp; 00118 00119 for (i=0;i<32;i++) 00120 dim[i] = p->dim[i]; 00121 00122 // currently only slices along the 2d axis. 00123 // in the future we could support horizontal 00124 // slicing for large 1d matrices 00125 if (w->id<(w->task->workercount-1)) { 00126 dim[1] = (dim[1]/w->task->workercount); 00127 offset = dim[1]*w->id; 00128 } else { 00129 // in case x->workercount is not a factor of dim[1] 00130 offset = (dim[1]/w->task->workercount)*(w->id); 00131 dim[1] = dim[1] - offset; 00132 } 00133 if (_jit_paralleldebug) 00134 post("wid %d, dim %d, offset %d",w->id, dim[1], offset); 00135 00136 pw.paralleldata = p; 00137 pw.workercount = w->task->workercount; 00138 pw.workerid = w->id; 00139 pw.offset[0] = 0; 00140 pw.offset[1] = offset; 00141 pw.extent[0] = dim[0]; 00142 pw.extent[1] = dim[1]; 00143 00144 splat.a[0] = (void *)p->data; 00145 splat.a[1] = (void *)p->dimcount; 00146 splat.a[2] = (void *)dim; 00147 splat.a[3] = (void *)p->planecount; 00148 for (i=0,j=4;i<p->iocount;i++) 00149 { 00150 // need to make a copy of the matrix info struct since we are overwriting some values 00151 minfo[i] = (t_jit_matrix_info *)jit_getbytes(sizeof(t_jit_matrix_info)); 00152 (*minfo[i]) = (*p->io[i].minfo); 00153 if (!(p->io[i].flags&JIT_PARALLEL_NDIM_FLAGS_FULL_MATRIX)) { 00154 bp = p->io[i].bp + p->io[i].minfo->dimstride[1] * offset; 00155 minfo[i]->dim[1] = MIN(dim[1],minfo[i]->dim[1]); 00156 } else { 00157 bp = p->io[i].bp; 00158 } 00159 splat.a[j++] = (void *)minfo[i]; 00160 splat.a[j++] = (void *)bp; 00161 } 00162 splat.a[j++] = (void *)&pw; 00163 if (dim[1]>0) 00164 (*(t_long_splat_fn)p->fn)(splat); 00165 for (i=0;i<p->iocount;i++) 00166 { 00167 jit_freebytes((char *)minfo[i],sizeof(t_jit_matrix_info)); 00168 } 00169 } 00170 00171 /** 00172 * Tasks one input/output N-dimensional matrix calcuations to multiple threads if appropriate. 00173 * This function fills out the t_jit_parallel_ndim struct 00174 * for a one input/output N-dimensional matrix calc 00175 * method, and calls jit_parallel_ndim_calc. This function 00176 * does not distinguish between what is an input or output. 00177 * 00178 * @ingroup parallelutilmod 00179 * 00180 * @param fn N-dimensional matrix calc method 00181 * @param data user defined pointer (typically object) 00182 * @param dimcount master number of dimensions to iterate 00183 * @param dim master pointer to dimension sizes 00184 * @param planecount master number of planes 00185 * @param minfo1 matrix info for first input/output 00186 * @param bp1 matrix data pointer for first input/output 00187 * @param flags1 parallel flags for first input/output 00188 * 00189 */ 00190 void jit_parallel_ndim_simplecalc1(method fn, void *data, long dimcount, long *dim, long planecount, t_jit_matrix_info *minfo1, char *bp1, long flags1) 00191 { 00192 t_jit_parallel_ndim p; 00193 00194 p.flags = 0; 00195 p.fn = fn; 00196 p.dimcount = dimcount; 00197 p.dim = dim; 00198 p.planecount = planecount; 00199 p.data = data; 00200 p.iocount = 1; 00201 00202 p.io[0].flags = flags1; 00203 p.io[0].minfo = minfo1; 00204 p.io[0].bp = bp1; 00205 00206 jit_parallel_ndim_calc(&p); 00207 } 00208 00209 /** 00210 * Tasks two input/output N-dimensional matrix calcuations to multiple threads if appropriate. 00211 * This function fills out the t_jit_parallel_ndim struct 00212 * for a two input/output N-dimensional matrix calc 00213 * method, and calls jit_parallel_ndim_calc. This function 00214 * does not distinguish between what is an input or output. 00215 * 00216 * @ingroup parallelutilmod 00217 * 00218 * @param fn N-dimensional matrix calc method 00219 * @param data user defined pointer (typically object) 00220 * @param dimcount master number of dimensions to iterate 00221 * @param dim master pointer to dimension sizes 00222 * @param planecount master number of planes 00223 * @param minfo1 matrix info for first input/output 00224 * @param bp1 matrix data pointer for first input/output 00225 * @param flags1 parallel flags for first input/output 00226 * @param minfo2 matrix info for second input/output 00227 * @param bp2 matrix data pointer for second input/output 00228 * @param flags2 parallel flags for second input/output 00229 * 00230 */ 00231 void jit_parallel_ndim_simplecalc2(method fn, void *data, long dimcount, long *dim, long planecount, t_jit_matrix_info *minfo1, char *bp1, 00232 t_jit_matrix_info *minfo2, char *bp2, long flags1, long flags2) 00233 { 00234 t_jit_parallel_ndim p; 00235 00236 p.flags = 0; 00237 p.fn = fn; 00238 p.dimcount = dimcount; 00239 p.dim = dim; 00240 p.planecount = planecount; 00241 p.data = data; 00242 p.iocount = 2; 00243 00244 p.io[0].flags = flags1; 00245 p.io[0].minfo = minfo1; 00246 p.io[0].bp = bp1; 00247 00248 p.io[1].flags = flags2; 00249 p.io[1].minfo = minfo2; 00250 p.io[1].bp = bp2; 00251 00252 jit_parallel_ndim_calc(&p); 00253 } 00254 00255 /** 00256 * Tasks three input/output N-dimensional matrix calcuations to multiple threads if appropriate. 00257 * This function fills out the t_jit_parallel_ndim struct 00258 * for a three input/output N-dimensional matrix calc 00259 * method, and calls jit_parallel_ndim_calc. This function 00260 * does not distinguish between what is an input or output. 00261 * 00262 * @ingroup parallelutilmod 00263 * 00264 * @param fn N-dimensional matrix calc method 00265 * @param data user defined pointer (typically object) 00266 * @param dimcount master number of dimensions to iterate 00267 * @param dim master pointer to dimension sizes 00268 * @param planecount master number of planes 00269 * @param minfo1 matrix info for first input/output 00270 * @param bp1 matrix data pointer for first input/output 00271 * @param flags1 parallel flags for first input/output 00272 * @param minfo2 matrix info for second input/output 00273 * @param bp2 matrix data pointer for second input/output 00274 * @param flags2 parallel flags for second input/output 00275 * @param minfo3 matrix info for third input/output 00276 * @param bp3 matrix data pointer for third input/output 00277 * @param flags3 parallel flags for third input/output 00278 * 00279 */ 00280 void jit_parallel_ndim_simplecalc3(method fn, void *data, long dimcount, long *dim, long planecount, t_jit_matrix_info *minfo1, char *bp1, 00281 t_jit_matrix_info *minfo2, char *bp2, t_jit_matrix_info *minfo3, char *bp3, long flags1, long flags2, long flags3) 00282 { 00283 t_jit_parallel_ndim p; 00284 00285 p.flags = 0; 00286 p.fn = fn; 00287 p.dimcount = dimcount; 00288 p.dim = dim; 00289 p.planecount = planecount; 00290 p.data = data; 00291 p.iocount = 3; 00292 00293 p.io[0].flags = flags1; 00294 p.io[0].minfo = minfo1; 00295 p.io[0].bp = bp1; 00296 00297 p.io[1].flags = flags2; 00298 p.io[1].minfo = minfo2; 00299 p.io[1].bp = bp2; 00300 00301 p.io[2].flags = flags3; 00302 p.io[2].minfo = minfo3; 00303 p.io[2].bp = bp3; 00304 00305 jit_parallel_ndim_calc(&p); 00306 } 00307 00308 /** 00309 * Tasks four input/output N-dimensional matrix calcuations to multiple threads if appropriate. 00310 * This function fills out the t_jit_parallel_ndim struct 00311 * for a three input/output N-dimensional matrix calc 00312 * method, and calls jit_parallel_ndim_calc. This function 00313 * does not distinguish between what is an input or output. 00314 * 00315 * @ingroup parallelutilmod 00316 * 00317 * @param fn N-dimensional matrix calc method 00318 * @param data user defined pointer (typically object) 00319 * @param dimcount master number of dimensions to iterate 00320 * @param dim master pointer to dimension sizes 00321 * @param planecount master number of planes 00322 * @param minfo1 matrix info for first input/output 00323 * @param bp1 matrix data pointer for first input/output 00324 * @param flags1 parallel flags for first input/output 00325 * @param minfo2 matrix info for second input/output 00326 * @param bp2 matrix data pointer for second input/output 00327 * @param flags2 parallel flags for second input/output 00328 * @param minfo3 matrix info for third input/output 00329 * @param bp3 matrix data pointer for third input/output 00330 * @param flags3 parallel flags for third input/output 00331 * @param minfo4 matrix info for fourth input/output 00332 * @param bp4 matrix data pointer for fourth input/output 00333 * @param flags4 parallel flags for fourth input/output 00334 * 00335 */ 00336 void jit_parallel_ndim_simplecalc4(method fn, void *data, long dimcount, long *dim, long planecount, t_jit_matrix_info *minfo1, char *bp1, 00337 t_jit_matrix_info *minfo2, char *bp2, t_jit_matrix_info *minfo3, char *bp3, t_jit_matrix_info *minfo4, char *bp4, 00338 long flags1, long flags2, long flags3, long flags4) 00339 { 00340 t_jit_parallel_ndim p; 00341 00342 p.flags = 0; 00343 p.fn = fn; 00344 p.dimcount = dimcount; 00345 p.dim = dim; 00346 p.planecount = planecount; 00347 p.data = data; 00348 p.iocount = 4; 00349 00350 p.io[0].flags = flags1; 00351 p.io[0].minfo = minfo1; 00352 p.io[0].bp = bp1; 00353 00354 p.io[1].flags = flags2; 00355 p.io[1].minfo = minfo2; 00356 p.io[1].bp = bp2; 00357 00358 p.io[2].flags = flags3; 00359 p.io[2].minfo = minfo3; 00360 p.io[2].bp = bp3; 00361 00362 p.io[3].flags = flags4; 00363 p.io[3].minfo = minfo4; 00364 p.io[3].bp = bp4; 00365 00366 jit_parallel_ndim_calc(&p); 00367 }
Copyright © 2008, Cycling '74