view DKU_Fns.c @ 0:efca8e7ec576

initial add -- working version
author Sean Halle <seanhalle@yahoo.com>
date Tue, 24 Sep 2013 08:08:18 -0700
parents
children
line source
1 /*
2 * Copyright 2009 OpenSourceResearchInstitute.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 */
10 #include <math.h>
11 #include <string.h>
12 #include "DKU__Test_App.h"
14 /*Bare smoke test of DKU wrapper library functions.
15 * Create one DKU instance, with a dummy kernel
16 * Bare bones divider and undivider
17 * simple root piece maker
18 * dummy serial kernel
19 */
21 int
22 square( int x )
23 { return x*x;
24 }
27 DKUPiece *
28 rootPieceMakerFn( void *params, DKUInstance *dkuInstance )
29 { DKUPiece *rootPiece;
31 rootPiece = PRServ__DKU_make_empty_DKU_piece();
32 rootPiece->parent = NULL;
33 rootPiece->payload = params;
34 rootPiece->dkuInstance = dkuInstance;
36 return rootPiece;
37 }
39 DKUPiece *
40 make_root_dku_piece_for_test_inst( int32 *data, int32 size,
41 DKUInstance *dkuInstance )
42 { DKUPiece *rootPiece;
43 TestAppStruct *params;
45 params = PR__malloc( sizeof(TestAppStruct) );
46 params->startIter = 0;
47 params->endIter = size-1;
48 params->data = data;
49 params->size = size;
50 rootPiece = (*(dkuInstance->rootPieceMaker))( params, dkuInstance );
51 return rootPiece;
52 }
55 void
56 kernelFn( void *_params, SlaveVP *animVP )
57 { int32 i;
58 DKUPiece *piece = (DKUPiece *)_params;
59 TestAppStruct *params = (TestAppStruct *)piece->payload;
60 int32 *data = params->data;
62 DEBUG__printf(dbgAppFlow, "Kernel %d", params->startIter);
64 for( i=params->startIter; i <= params->endIter; ++i )
65 { data[i] = square(i);
66 }
67 PRServ__DKU_end_kernel( piece, animVP );
68 }
71 /*Serial kernel is called with bare app-defined struct, not DKUPiece
72 */
73 void
74 serialKernelFn( void *_params, SlaveVP *animVP )
75 { int32 i;
76 TestAppStruct *params = (TestAppStruct *)_params;
77 int32 *data = params->data;
79 DEBUG__printf(dbgAppFlow, "SerialKernel %d", params->startIter);
81 for( i=params->startIter; i < params->endIter; ++i )
82 { data[i] = square(i);
83 }
84 PRServ__DKU_end_serial_kernel( animVP );
85 }
87 void
88 dividerFn( DKUPiece *pieceToDivide )
89 {
90 int32 idx, childIdx, numChildren, size, *data;
91 DKUPiece *childPiece, **childrenArray;
92 TestAppStruct *params;
93 DEBUG__printf(dbgAppFlow, "divider %p", pieceToDivide)
95 numChildren = pieceToDivide->numChildren;
96 childrenArray = PR__malloc( numChildren * sizeof(DKUPiece *) );
97 size = ((TestAppStruct*)pieceToDivide->payload)->size;
98 data = ((TestAppStruct*)pieceToDivide->payload)->data;
99 int chunkSize = size/numChildren; //for portability, lang would choose this dynamically at run time
100 //note: should do processing to catch too small a chunksize and deal w/it
101 for( childIdx = 0; childIdx < numChildren; childIdx++ )
102 { params = PR__malloc(sizeof(TestAppStruct)); //these define work the task performs
103 idx = childIdx * chunkSize;
104 params->startIter = idx;
105 params->endIter = idx + chunkSize -1;
106 params->data = data;
107 params->size = size;
108 childPiece = PRServ__DKU_make_child_piece_from( pieceToDivide );
109 childPiece->payload = params;
110 childrenArray[childIdx] = childPiece;
111 }
112 params->endIter = size -1; //catch truncation error from chunkSize calc
114 pieceToDivide->children = childrenArray;
115 }
117 void undividerFn( DKUPiece *piece )
118 {
119 //DEBUG__printf(dbgAppFlow, "Undivider %p", piece)
120 //nothing to do -- request handler does counting of completions
121 }