| 1 | #include <stdio.h>
|
|---|
| 2 | #include <stdlib.h>
|
|---|
| 3 | #include <string.h>
|
|---|
| 4 |
|
|---|
| 5 | /*
|
|---|
| 6 | If increase MAX_DATA_SIZE, nothing happens if DATA_SIZE is given a concrete value
|
|---|
| 7 | If I increase DATA_SIZE to 2, I get
|
|---|
| 8 | Computed '2/2' correct values!
|
|---|
| 9 | Computed '1/2' correct values!
|
|---|
| 10 | */
|
|---|
| 11 | $input int DATA_SIZE;
|
|---|
| 12 | $input int NUM_DEVICES;
|
|---|
| 13 | $input int MAX_DATA_SIZE;
|
|---|
| 14 | $input int MAX_NUM_DEVICES;
|
|---|
| 15 | $assume DATA_SIZE && DATA_SIZE < MAX_DATA_SIZE;
|
|---|
| 16 | $assume NUM_DEVICES < MAX_NUM_DEVICES;
|
|---|
| 17 | $gbarrier gbarrier = $gbarrier_create($here, NUM_DEVICES);
|
|---|
| 18 | //struct goes here
|
|---|
| 19 | typedef struct
|
|---|
| 20 | {
|
|---|
| 21 | int device_id;
|
|---|
| 22 | //other variables
|
|---|
| 23 | float * input;
|
|---|
| 24 | float * output;
|
|---|
| 25 | int count;
|
|---|
| 26 | }process;
|
|---|
| 27 |
|
|---|
| 28 | //kernel goes here
|
|---|
| 29 | void square(float* input, float* output, const unsigned int count)
|
|---|
| 30 | {
|
|---|
| 31 | //int i = get_global_id(0);
|
|---|
| 32 | int i = 0;
|
|---|
| 33 | if (i < count)
|
|---|
| 34 | {
|
|---|
| 35 | output[i] = input[i] * input[i];
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | /*
|
|---|
| 39 | Note that the original lines were "__kernel void square( \n" \
|
|---|
| 40 | " __global float* input, \n" \
|
|---|
| 41 | " __global float* output, \n" \
|
|---|
| 42 | " const unsigned int count) \n" \
|
|---|
| 43 |
|
|---|
| 44 | Any parser must take note of and don't input \n, "", or \ as is
|
|---|
| 45 | __global float * input, __global float * output, int count;
|
|---|
| 46 | */
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | int main(int argc, char** argv)
|
|---|
| 50 | {
|
|---|
| 51 | //get the number from clGetDeviceIDs 3rd parameter
|
|---|
| 52 | //int num_devices = 1;
|
|---|
| 53 |
|
|---|
| 54 | //variables from __kernel come here
|
|---|
| 55 | float * input;
|
|---|
| 56 | float * output;
|
|---|
| 57 | int count;
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 | //from the code before
|
|---|
| 61 | float data[DATA_SIZE]; // original data set given to device
|
|---|
| 62 | float results[DATA_SIZE]; // results returned from device
|
|---|
| 63 | int correct; // number of correct results returned
|
|---|
| 64 | //handle the definitions being put in different places
|
|---|
| 65 |
|
|---|
| 66 | int i = 0;
|
|---|
| 67 | count = DATA_SIZE; //count defined here
|
|---|
| 68 | for(i = 0; i < count; i++)
|
|---|
| 69 | {
|
|---|
| 70 | data[i] = rand() / (float)RAND_MAX;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | //comes from clCreateBuffer
|
|---|
| 76 | input = (float *) malloc(sizeof(float) * count);
|
|---|
| 77 | output = (float *) malloc(sizeof(float) * count);
|
|---|
| 78 |
|
|---|
| 79 | //Possibly keep a list of variables, with a flag for whether they are init or not
|
|---|
| 80 | //Not init, malloc one from what is found in
|
|---|
| 81 | //output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * count, NULL, NULL);
|
|---|
| 82 |
|
|---|
| 83 | input = data; //input defined here
|
|---|
| 84 | //came from clEnqueueWriteBuffer rather than the start of code
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | //Phase after this is the definitions
|
|---|
| 88 | process now[NUM_DEVICES];
|
|---|
| 89 | //put device_ids
|
|---|
| 90 | for(int i = 0; i < NUM_DEVICES; i++)
|
|---|
| 91 | {
|
|---|
| 92 | now[i].device_id = i;
|
|---|
| 93 | //other variables
|
|---|
| 94 | now[i].input = input;
|
|---|
| 95 | now[i].output = output;
|
|---|
| 96 | now[i].count = count;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | //spawns processes according to devices, uses the struct for inputs
|
|---|
| 100 | $proc procs[NUM_DEVICES];
|
|---|
| 101 | for(int i = 0; i < NUM_DEVICES; i++)
|
|---|
| 102 | {
|
|---|
| 103 | procs[i] = $spawn square(now[i].input, now[i].output, now[i].count);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | for(int i = 0; i < NUM_DEVICES; i++)
|
|---|
| 107 | {
|
|---|
| 108 | $wait(procs[i]);
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | //$barrier barrier = $barrier_create($here, gbarrier, now[i].device_id);
|
|---|
| 112 | //$barrier_call(barrier);
|
|---|
| 113 | //$barrier_destroy(barrier);
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | $gbarrier_destroy(gbarrier);
|
|---|
| 118 |
|
|---|
| 119 | //use the information from clEnqueueReadBuffer
|
|---|
| 120 | //may have to alter later
|
|---|
| 121 | for(int i = 0; i < count; i++)
|
|---|
| 122 | {
|
|---|
| 123 | results[i] = output[i];
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | correct = 0;
|
|---|
| 127 | for(i = 0; i < count; i++)
|
|---|
| 128 | {
|
|---|
| 129 | if(results[i] == data[i] * data[i])
|
|---|
| 130 | {
|
|---|
| 131 | correct++;
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | // Print a brief summary detailing the results
|
|---|
| 136 | //
|
|---|
| 137 | printf("Computed '%d/%d' correct values!\n", correct, count);
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 | return 0;
|
|---|
| 141 | }
|
|---|