/** * The actual installation of the content handler * @return <code>JSR211_OK</code> if the installation completes successfully. */ static jsr211_result installHandler(int n) { JSR211_content_handler ch = JSR211_CONTENT_HANDLER_INITIALIZER; char *ptr = rowHandlers[n]; jsr211_result status = JSR211_FAILED; int anm_num; // buffer for actionname map length /* * Fill up CH data: * ID from handlerIds. * Others from rowHandlers: * <suite_ID> -- if this line is empty, the handler's flag is NATIVE * <classname> -- if the handler's flag is NATIVE here is executive path * <type1 type2 type3 ...> -- array of types devided by whitespases * <suffix1 suffix2 suffix3 ...> -- suffixes (see types) * <locale1 locale2 locale3 ...> -- locales (see types) * <actionName11 actionName21 ...> -- action_name_i_j is for * action_i and locale_j * <access1 access2 access3 ...> -- accesses (see types) */ if (PCSL_STRING_OK == pcsl_string_dup(handlerIds[n], &(ch.id)) && fillString(&ptr, &ch.class_name) && fillArray(&ptr, &ch.type_num, &ch.types) && fillArray(&ptr, &ch.suff_num, &ch.suffixes) && fillArray(&ptr, &ch.act_num, &ch.actions) && fillArray(&ptr, &ch.locale_num, &ch.locales) && fillArray(&ptr, &anm_num, &ch.action_map) && fillArray(&ptr, &ch.access_num, &ch.accesses) && anm_num == (ch.act_num * ch.locale_num)) { ch.suite_id = INTERNAL_SUITE_ID; ch.flag = JSR211_FLAG_COMMON; status = jsr211_register_handler(&ch); #if REPORT_LEVEL <= LOG_CRITICAL } else { REPORT_CRIT(LC_NONE, "jsr211_deploy.c: handler data parsing failed"); #endif } // clean up handler's memory jsr211_cleanHandlerData(&ch); return status; }
// main() is the entry point of the program. int main(int argc, char* argv[]) { // Create a new array capable of storing 10 elements // and fill it with values using the function declared // above. Arrays declared in this manner are located on // the stack, which is where statically allocated (as // in not at runtime) memory is stored. int array[10]; // The "array" that we pass here is actually a pointer // to a block of memory capable of storing 10 integers. // array[0] is the first integer in this block of // memory, array[1] is the second, and so on. Since // C does not track array lengths, we have to specify // how many elements the array contains. // // TODO(1): What happens if the second argument is set // to 11 instead? How about 100? 1000? Make sure to set // the second argument back to 10 when you are done // testing. // Answer:Buffer Overflow will occur.The values will be written to locations from (array) to (array+(10*sizeof(int)). // For Value -100 - Buffer over flow error occurs.The values will be written from locations (array) to (array+(100*sizeof(int)). // for value - n && (n > 10) - Buffer over flow occurs.The values will be written from locations (array) to (array+(n*sizeof(int)). fillArray(array, 10); int value; // In C, we can take the address of something using the // & operator. &value is of the type int*, meaning that // it is a pointer to an integer (as in it stores the // address in memory of where the actual int is located). // // TODO(2): We can actually use the address of the value // declared here as if it were an array of a single // element; why is this possible? // Answer:The machine interprets both in similar manner.A single integer and an int array with single element // has same size (= size of int).In C it checks type of argument passed to the function. In the above mentioned scenario // both (&value - pointer to Integer and (considering array - int array with single element) array[0] - pointer to intger) // are same. fillArray(&value, 1); // fillArray should set value to 0 * 3 + 2 = 2. assert(value == 2); // The following creates an instance of FourInts on the // stack. FourInts is really just an array of four ints, // although we can refer to the ints stored in it by // name as well. FourInts four_ints; // Set the first int to have a value of 0 and verify // that the value changed. four_ints.a = 0; assert(four_ints.a == 0); // Depending on whether or not you like to live // dangerously, the following is either exciting or // terrifying. Though &four_ints is of type FourInts* // (as in a pointer to a FourInts struct), we can // use a cast to pretend that it is actually an array // of integers instead. fillArray((int*) &four_ints, 4); // We can confirm that fillArray updated the values // in the FourInts struct: assert(four_ints.a == 2); assert(four_ints.b == 5); assert(four_ints.c == 8); assert(four_ints.d == 11); // In the case that the size of an array is not known // until runtime, the malloc() function can be used to // allocate memory dynamically. Memory that is // allocated dynamically is stored on the heap, which // is separate from the stack. C is unlike Java, // however, in that dynamically-allocated memory must // be freed explicitly when the program is done using // it via the free() function. malloc() takes a single // argument, which is the number of bytes to allocate. // sizeof(int) gives how many bytes an int contains // (which is four), so sizeof(int) * 5 is 20. int* heap_array = (int*) malloc(sizeof(int) * 5); fillArray(heap_array, 5); // Now that we have finished with the heap-allocated // array, free() the memory associated with it. // // TODO(3): What happens if we remove the free() // statement below? Try running "valgrind ./arrays" // after compiling the program both with and without // it. valgrind is a tool for analyzing how programs // use memory, which is often invaluable for C and // C++ programming. // Answer: If we remove the following free statement then the memory allocated for heap_array(20 bytes) // will be lost and that memory cannot be used for further use.If dynamically allocate memory withou freeing it after use will // lead to dangerous consequenses(Application will crash). free(heap_array); // TODO(4): Now it's your turn to write some code. // Using malloc(), allocate a FourInts struct // dynamically (on the heap) and use fillArray to // populate it with values. Make sure to free the // memory when you are done, and use the valgrind // tool mentioned above to check that there aren't // any errors. As a "sanity check," add four assert // statements to verify that the a, b, c, and d // fields of the FourInts struct are set to what // you would expect. (Hint, you'll need to use the // -> operator to access fields of a FourInts* // variable instead of the . operator). int* heap_four_struct = (int*) malloc(sizeof(int) * 4); fillArray(heap_four_struct, 4); // heap_four_struct type casted from (int *) to (FourInts *) assert(((FourInts*)heap_four_struct)->a == 2); assert(((FourInts*)heap_four_struct)->b == 5); assert(((FourInts*)heap_four_struct)->c == 8); assert(((FourInts*)heap_four_struct)->d == 11); // free the heap free(heap_four_struct); return 0; }
// main() is the entry point of the program. int main(int argc, char* argv[]) { // Create a new array capable of storing 10 elements // and fill it with values using the function declared // above. Arrays declared in this manner are located on // the stack, which is where statically allocated (as // in not at runtime) memory is stored. int array[10]; // The "array" that we pass here is actually a pointer // to a block of memory capable of storing 10 integers. // array[0] is the first integer in this block of // memory, array[1] is the second, and so on. Since // C does not track array lengths, we have to specify // how many elements the array contains. // // TODO(1): What happens if the second argument is set // to 11 instead? How about 100? 1000? Make sure to set // the second argument back to 10 when you are done // testing. // Answer: fillArray(array, 10); int value; // In C, we can take the address of something using the // & operator. &value is of the type int*, meaning that // it is a pointer to an integer (as in it stores the // address in memory of where the actual int is located). // // TODO(2): We can actually use the address of the value // declared here as if it were an array of a single // element; why is this possible? // Answer: fill array takes a pointer as input variable and // the pointer to single element &value satisfies the function // arguments. It operates on the single element as one element // array and performs the required computations. fillArray(&value, 1); // fillArray should set value to 0 * 3 + 2 = 2. assert(value == 2); // The following creates an instance of FourInts on the // stack. FourInts is really just an array of four ints, // although we can refer to the ints stored in it by // name as well. FourInts four_ints; // Set the first int to have a value of 0 and verify // that the value changed. four_ints.a = 0; assert(four_ints.a == 0); // Depending on whether or not you like to live // dangerously, the following is either exciting or // terrifying. Though &four_ints is of type FourInts* // (as in a pointer to a FourInts struct), we can // use a cast to pretend that it is actually an array // of integers instead. fillArray((int*) &four_ints, 4); // We can confirm that fillArray updated the values // in the FourInts struct: assert(four_ints.a == 2); assert(four_ints.b == 5); assert(four_ints.c == 8); assert(four_ints.d == 11); // In the case that the size of an array is not known // until runtime, the malloc() function can be used to // allocate memory dynamically. Memory that is // allocated dynamically is stored on the heap, which // is separate from the stack. C is unlike Java, // however, in that dynamically-allocated memory must // be freed explicitly when the program is done using // it via the free() function. malloc() takes a single // argument, which is the number of bytes to allocate. // sizeof(int) gives how many bytes an int contains // (which is four), so sizeof(int) * 5 is 20. int* heap_array = (int*) malloc(sizeof(int) * 5); fillArray(heap_array, 5); // Now that we have finished with the heap-allocated // array, free() the memory associated with it. // // TODO(3): What happens if we remove the free() // statement below? Try running "valgrind ./arrays" // after compiling the program both with and without // it. valgrind is a tool for analyzing how programs // use memory, which is often invaluable for C and // C++ programming. // Answer: on removing/ commenting the free() function // below, valgrind shows a memory leak. Though there is // 1 alloc shown it is not freed up resulting in 0 frees. // However, using the free command avoids this possible // memory leak that can occur. free(heap_array); // TODO(4): Now it's your turn to write some code. // Using malloc(), allocate a FourInts struct // dynamically (on the heap) and use fillArray to // populate it with values. Make sure to free the // memory when you are done, and use the valgrind // tool mentioned above to check that there aren't // any errors. As a "sanity check," add four assert // statements to verify that the a, b, c, and d // fields of the FourInts struct are set to what // you would expect. (Hint, you'll need to use the // -> operator to access fields of a FourInts* // variable instead of the . operator). FourInts* struct_arary = malloc(sizeof(FourInts)); fillArray((int*) struct_arary,4); assert((struct_arary->a)==2); assert((struct_arary->b)==5); assert((struct_arary->c)==8); assert((struct_arary->d)==11); free(struct_arary); return 0; }
Array1d::Array1d(int n){ if(n<2)n=2; size=n; array=fillArray(); }
// main() is the entry point of the program. int main(int argc, char* argv[]) { // Create a new array capable of storing 10 elements // and fill it with values using the function declared // above. Arrays declared in this manner are located on // the stack, which is where statically allocated (as // in not at runtime) memory is stored. int array[10]; // The "array" that we pass here is actually a pointer // to a block of memory capable of storing 10 integers. // array[0] is the first integer in this block of // memory, array[1] is the second, and so on. Since // C does not track array lengths, we have to specify // how many elements the array contains. // // TODO(1): What happens if the second argument is set // to 11 instead? How about 100? 1000? Make sure to set // the second argument back to 10 when you are done // testing. // Answer: When sec. arg is set to 11, then we have 11-th address value inside array, // else if set to 100 we get "Segmentation fault", //we get en error with permission to get more memory //than declared when array was created, as above but, // we want to many not used memory to read. // -1000 in this case we getting out from program //reserved memory so jobs after that fill won't be execute! // last two situations get fault but firt will be still correct for // gcc until already used memory with next index id won't be read. fillArray(array, 10); int value; // In C, we can take the address of something using the // & operator. &value is of the type int*, meaning that // it is a pointer to an integer (as in it stores the // address in memory of where the actual int is located). // // TODO(2): We can actually use the address of the value // declared here as if it were an array of a single // element; why is this possible? // Answer: Because address of e.g. int value is like first value from array where // address to first element is similarly address for another int value. fillArray(&value, 1); // fillArray should set value to 0 * 3 + 2 = 2. assert(value == 2); // The following creates an instance of FourInts on the // stack. FourInts is really just an array of four ints, // although we can refer to the ints stored in it by // name as well. FourInts four_ints; // Set the first int to have a value of 0 and verify // that the value changed. four_ints.a = 0; assert(four_ints.a == 0); // Depending on whether or not you like to live // dangerously, the following is either exciting or // terrifying. Though &four_ints is of type FourInts* // (as in a pointer to a FourInts struct), we can // use a cast to pretend that it is actually an array // of integers instead. fillArray((int*) &four_ints, 4); // We can confirm that fillArray updated the values // in the FourInts struct: assert(four_ints.a == 2); assert(four_ints.b == 5); assert(four_ints.c == 8); assert(four_ints.d == 11); // In the case that the size of an array is not known // until runtime, the malloc() function can be used to // allocate memory dynamically. Memory that is // allocated dynamically is stored on the heap, which // is separate from the stack. C is unlike Java, // however, in that dynamically-allocated memory must // be freed explicitly when the program is done using // it via the free() function. malloc() takes a single // argument, which is the number of bytes to allocate. // sizeof(int) gives how many bytes an int contains // (which is four), so sizeof(int) * 5 is 20. int* heap_array = (int*) malloc(sizeof(int) * 5); fillArray(heap_array, 5); // Now that we have finished with the heap-allocated // array, free() the memory associated with it. // // TODO(3): What happens if we remove the free() // statement below? Try running "valgrind ./arrays" // after compiling the program both with and without // it. valgrind is a tool for analyzing how programs // use memory, which is often invaluable for C and // C++ programming. // // Answer: When method free on heap_array is not used, // then program lost some of the memory, especially 20 bytes // (struct memory -> 5 times 4 bytes as int size in this example) in 1 block. // This causing memory leaks, which is bad experience when the program ends. free(heap_array); // TODO(4): Now it's your turn to write some code. // Using malloc(), allocate a FourInts struct // dynamically (on the heap) and use fillArray to // populate it with values. Make sure to free the // memory when you are done, and use the valgrind // tool mentioned above to check that there aren't // any errors. As a "sanity check," add four assert // statements to verify that the a, b, c, and d // fields of the FourInts struct are set to what // -> operator to access fields of a FourInts* // variable instead of the . operator). int* my_malloc = (int*) malloc(sizeof(int) * 14); fillArray(my_malloc, 14); my_malloc[3] = 678; assert(my_malloc[3] == 678); free(my_malloc); int bb = 4; four_ints.b = bb; assert( four_ints.b == 4); FourInts* struct_pointer = &four_ints; int* temp_pointer = (int*) &struct_pointer -> b ; printf("fi.b: %p tp: %p sp.b : %p \n", &four_ints.b, temp_pointer, (int*) &struct_pointer -> b); int b = 9; *temp_pointer = b; assert( four_ints.b == 9 ); FourInts other_struct; //printf("size of other_struct : %lu \n", sizeof(other_struct)); int* mal = (int*) malloc(sizeof(other_struct)); fillArray(mal, 4); for ( int i = 0; i < 4; i++){ int temp = i * 3 + 2; assert( mal[i] == temp); } free(mal); return 0; }
int main(int args, char **argv) { int num_args_req = 3; if (args != num_args_req) { args_check(num_args_req); } int n = atoi(argv[1]); // leading size of matrix char filename[30]; // name of the file strcpy(filename, &argv[2][0]); // Create Arrays to hold matrices double **A = createArray(n, n); double **B = createArray(n, n); double **C = createArray(n, n); int i = 0; int j = 0; int k = 0; //set time structs struct timeval tv1, tv2; struct timezone tz; // open a file to write to FILE *f = createFile(filename); // Fill matrices A and B fillArray(A, n, 1.); fillArray(B, n, 1.); fillArray(C, n, 0.); printMatrix(A,n); printMatrix(B,n); printMatrix(C,n); //get starting time gettimeofday(&tv1, &tz); #pragma omp parallel { Multiply(n, A, B, C); } printMatrix(C,n); double max_sum = 0.0; double *c = C[0]; double col_sum = 0.0; #pragma omp parallel for shared(max_sum,n,i,j,c) for (j = 0; j < n; j++) { col_sum = 0.0; #pragma omp parallel for private(col_sum) for (i = 0; i < n; i++) { printf("Adding %f to sum of col %d\n\n", c[i*n+j],j); col_sum += c[i*n+j]; #pragma omp critical if(col_sum>max_sum){ max_sum=col_sum; } } //printf("col_sum for: c[%d] is: %f \n",j,col_sum); } //complete time measurement gettimeofday(&tv2, &tz); double elapsed = (double) (tv2.tv_sec - tv1.tv_sec) + (double) (tv2.tv_usec - tv1.tv_usec) * 1.e-6; fprintf(f, "%d %f\n", n, elapsed); printf("Max col norm is: %f\n\n", max_sum); return 0; }
void hang() { liftTaskDelete(); // engage the winch digitalWrite(winchPiston, 1); // give it a chance to engage taskDelay(100); int leftTicks = analogRead(potLiftLeft); int rightTicks = analogRead(potLiftRight); int changesArrayLength = STALL_TIME/HANG_DT; int changes[changesArrayLength]; // -1 indicates no data fillArray(changes, changesArrayLength, -1); int stallTicks = 0; while (true) { if (joystickGetDigital(1, 8, JOY_DOWN)) { return; } int newLeftTicks = analogRead(potLiftLeft); int newRightTicks = analogRead(potLiftRight); if (newLeftTicks < HANG_HEIGHT && newRightTicks < HANG_HEIGHT) { // we're at the right height, stop break; } if ((stallTicks * HANG_DT) > STALL_DELAY) { // do stall detection int change = abs(leftTicks - newLeftTicks) + abs(rightTicks - newRightTicks); pushArrayValue(changes, changesArrayLength, change); int totalChanges = sumStallChanges(changes, changesArrayLength); // if changes has data (>= 0) and it's stalled, stop motors if (totalChanges >= 0 && totalChanges < STALL_THRESHOLD) { // stalled, wait 3 seconds hangMotors(0); delay(3000); // clear changes fillArray(changes, changesArrayLength, -1); continue; } } leftTicks = newLeftTicks; rightTicks = newRightTicks; hangMotors(127); stallTicks++; taskDelay(HANG_DT); } hangMotors(0); }
bool BinTreeNodeReader::readString(int token, QByteArray& s) { if (token == -1) { throw ProtocolException("-1 token in readString."); } if (token > 0 && token < 0xf5) return getToken(token, s); QByteArray user,server; bool usr, srv; //no default value. switch (token) { case 0: return false; case 0xfc: quint8 size8; if (!readInt8(size8)) return false; return fillArray(s,size8); case 0xfd: qint32 size24; if (!readInt24(size24)) return false; return fillArray(s,size24); case 0xfe: quint8 token8; if (!readInt8(token8)) return false; return getToken(0xf5 + token8, s); case 0xfa: usr = readString(user); srv = readString(server); if (usr & srv) { s = user + "@" + server; return true; } if (srv) { s = server; return true; } throw ProtocolException("readString couldn't reconstruct jid."); case 0xff: { quint8 nbyte; if (!readInt8(nbyte)) return false; int size = nbyte & 0x7f; int numnibbles = size * 2 - ((nbyte & 0x80) ? 1 : 0); QByteArray res; if (!fillArray(res, size)) return false; res = res.toHex().left(numnibbles); res = res.replace('a', '-').replace('b', '.'); s = res; return true; } } throw ProtocolException("readString invalid token " + QString::number(token)); }
int main() { //variable declrations int myArray[50], myArray2[] = {1, 2, 3, 4, 5}, myArray3[] = {1, 2, 3, 4, 5}; int index, searchResults, key; //testing fillArray fillArray(myArray, 50, 1000); //testing printNperline() printf("Before sort printing 10 per line\n"); printNperline(myArray, 50, 10); printf("\n"); printf("Before sort printing 4 per line\n"); printNperline(myArray, 50, 4); printf("\n"); printf("Before sort printing 6 per line\n"); printNperline(myArray, 50, 6); //testing bubble sort on myArray bubbleSort(myArray, 50); //printing myArray after sort printf("\n"); printf("After sort printing 6 per line\n"); printNperline(myArray, 50, 6); printf("\n"); printf("After sort printing 15 per line\n"); printNperline(myArray, 50, 15); printf("\n"); printf("After sort printing 11 per line\n"); printNperline(myArray, 50, 11); //testing linear search printf("\n"); printf("Testing linear search for 82\n"); key = 82; searchResults = linearSearch(myArray, 50, key); if(searchResults >= 0) printf("The key value %d was found in myArray at position %d\n", key, searchResults); else printf("The key value %d was not found in myArray\n", key); printf("\n"); printf("Testing linear search for %d which is the last element in the array\n", myArray[49]); key = myArray[49]; searchResults = linearSearch(myArray, 50, key); if(searchResults >= 0) printf("The key value %d was found in myArray at position %d\n", key, searchResults); else printf("The key value %d was not found in myArray\n", key); printf("\n"); printf("Testing linear search for %d which is the first element in the array\n", myArray[0]); key = myArray[0]; searchResults = linearSearch(myArray, 50, key); if(searchResults >= 0) printf("The key value %d was found in myArray at position %d\n", key, searchResults); else printf("The key value %d was not found in myArray\n", key); //testing shiftElements printf("\n"); printf("Shifting the elements in the array right 6 elements\n"); shiftElements(myArray, 50, 6); printNperline(myArray, 50, 10); printf("\n"); printf("Shifting the elements in the array left 4 elements\n"); shiftElements(myArray, 50, -4); printNperline(myArray, 50, 10); }
double C_measure::error_fiber_tilde(CtlStruct* phi) { //compute the length of the fiber //compute Reimann integrale (approximation) unsigned long N=0; double S = 0.0, phiX, phiY, phiZ, sumDelta=0.0, phiXdev, phiYdev, phiZdev, tmpS, tmpL; double** D = new double*[3]; D[0] = new double[3]; D[1] = new double[3]; D[2] = new double[3]; for(unsigned long i=0 ; i<(phi->N_element-1) ; i++) { //calculate speed vector phiX = phi->elts[3*(i+1)]-phi->elts[3*i]; phiY = phi->elts[3*(i+1)+1]-phi->elts[3*i+1]; phiZ = phi->elts[3*(i+1)+2]-phi->elts[3*i+2]; tmpL = sqrt(SQR(phiX) + SQR(phiY) + SQR(phiZ)); ///check norm of speed vector/// if(!(isnan(tmpL) || isinf(tmpL) || (tmpL<SMALL_NUM))) { //get tensor at point (phi->elts[3*i], phi->elts[3*i+2], phi->elts[3*i+2]) fillArray(phi->elts[3*i], phi->elts[3*i+1], phi->elts[3*i+2], D);//must check what is in D //calculates tensor product phiXdev = D[0][0]*phiX + D[0][1]*phiY + D[0][2]*phiZ; phiYdev = D[1][0]*phiX + D[1][1]*phiY + D[1][2]*phiZ; phiZdev = D[2][0]*phiX + D[2][1]*phiY + D[2][2]*phiZ; //calculates eigen values C_toolbox_eigen_sym v((double**) D, 3, false); tmpS = sqrt(SQR(phiXdev) + SQR(phiYdev) + SQR(phiZdev))/v.d[0]; if(!isnan(tmpS) && !isinf(tmpS)) { //integrate S += tmpS; //sum the length sumDelta +=tmpL; } else { cout << "tmpS nan or inf " << tmpS << endl; N++; } } else { cout << "tmpL nan or inf " << tmpL << endl; N++; } } //deletion delete D[2]; delete D[1]; delete D[0]; delete D; if(N<(phi->N_element-1)) return (sumDelta-S)/SQR(sumDelta); else return -1.0; }
int main(int argc, char **argv) { int size=10000; if (argc>1) { size=atoi(argv[1]); } double **array; int nthreads; //The following block should be executed in parallel, use barriers where necessary { #pragma omp parallel { timeval t_start,t_end; nthreads=omp_get_num_threads(); int i=omp_get_thread_num(); #pragma omp master { // The top level of the array should be allocated by the master thread printf("Started %i threads\n",nthreads); array=(double**)malloc(nthreads*sizeof(double*)); } #pragma omp barrier //Each thread allocates the internal memory array[i]=(double*)malloc(size*sizeof(double)); #pragma omp barrier //Each thread fills its own array (10 iterations for better timing) gettimeofday(&t_start,0); for (int j=0;j<10;j++) fillArray(array[i],size,i); gettimeofday(&t_end,0); double time=((t_end.tv_usec+t_end.tv_sec*1e6)-(t_start.tv_usec +t_start.tv_sec*1e6))/1e6/10; printf("Fill Array: %i: %f\n",i,time); #pragma omp barrier //Each thread i should do the following summation of the array of thread i+1 (again 10 iterations for better timing) gettimeofday(&t_start,0); double sum; for (int j=0;j<10;j++) { sum=0.0; for (int k=0;k<size;k++) { sum+=array[(i+1)%nthreads][k]; } } gettimeofday(&t_end,0); time=((t_end.tv_usec+t_end.tv_sec*1e6)-(t_start.tv_usec+t_start.tv_sec*1e6))/1e6/10; printf("%i: Sum %f! Time: %f [accessed array %i]\n",i,sum,time,(i+1)%nthreads); #pragma omp barrier //Each thread frees its internal array free(array[i]); #pragma omp barrier #pragma omp master { //now free the toplevel array free(array); } } } return 0; }
double C_measure::error_fiber(CtlStruct* phi) { #ifdef SHOULD_NOT_BE_USED unsigned long N = phi->N_element; //Point* p = G->getPoints(); double TphiPrime; double** a = new double*[3]; a[0] = new double[3]; a[1] = new double[3]; a[2] = new double[3]; // C_toolbox_eigen_sym* v; double phiPrimeX, phiPrimeY, phiPrimeZ, Z; double E = 0; double dLength=0.0; unsigned long dN=0; //iter for(unsigned long i=1 ; i<N-1 ; i++) { fillArray(phi->elts[3*i], phi->elts[3*i+1], phi->elts[3*i+2], a); phiPrimeX = phi->elts[3*(i+1)] - phi->elts[3*(i-1)]; phiPrimeY = phi->elts[3*(i+1)+1] - phi->elts[3*(i-1)+1]; phiPrimeZ = phi->elts[3*(i+1)+2] - phi->elts[3*(i-1)+2]; Z = sqrt(SQR(phiPrimeX) + SQR(phiPrimeY) + SQR(phiPrimeZ)); phiPrimeX /= Z; phiPrimeY /= Z; phiPrimeZ /= Z; if(isnan(phiPrimeX) || isnan(phiPrimeY) || isnan(phiPrimeZ) || isinf(phiPrimeX) || isinf(phiPrimeY) || isinf(phiPrimeZ)) { E = -1.0; i = N; //cout << "phiPrime" << endl; } else { TphiPrime = TphiPrimeNorm2(phi->elts[3*i], phi->elts[3*i+1], phi->elts[3*i+2], phiPrimeX, phiPrimeY, phiPrimeZ); if(TphiPrime<0.0) { dLength += 0.5*Z;//sqrt(SQR(phi->elts[3*(i+1)] - phi->elts[3*(i-1)]) + SQR(phi->elts[3*(i+1)+1] - phi->elts[3*(i-1)+1]) + SQR(phi->elts[3*(i+1)+2] - phi->elts[3*(i-1)+2])); //dLength += sqrt(SQR(phi->elts[3*(i)] - phi->elts[3*(i-1)]) + SQR(phi->elts[3*(i)+1] - phi->elts[3*(i-1)+1]) + SQR(phi->elts[3*(i)+2] - phi->elts[3*(i-1)+2])); dN++; //cout << "TphiPrime" << endl; } else { v = new C_toolbox_eigen_sym((/**const*/ double**) a, 3, false); if(isnan(1-(TphiPrime/(v->d[0]))) || isinf(1-(TphiPrime/(v->d[0])))) { dLength += 0.5*Z;//sqrt(SQR(phi->elts[3*(i+1)] - phi->elts[3*(i-1)]) + SQR(phi->elts[3*(i+1)+1] - phi->elts[3*(i-1)+1]) + SQR(phi->elts[3*(i+1)+2] - phi->elts[3*(i-1)+2])); //dLength += sqrt(SQR(phi->elts[3*(i)] - phi->elts[3*(i-1)]) + SQR(phi->elts[3*(i)+1] - phi->elts[3*(i-1)+1]) + SQR(phi->elts[3*(i)+2] - phi->elts[3*(i-1)+2])); dN++; //cout << "some nan" << endl; } else { if(ABS(v->d[0])>SMALL_NUM) E += (1-(TphiPrime/(v->d[0]))); /// because ||phi'||=1 else { dLength += 0.5*Z;//sqrt(SQR(phi->elts[3*(i+1)] - phi->elts[3*(i-1)]) + SQR(phi->elts[3*(i+1)+1] - phi->elts[3*(i-1)+1]) + SQR(phi->elts[3*(i+1)+2] - phi->elts[3*(i-1)+2])); //dLength += sqrt(SQR(phi->elts[3*(i)] - phi->elts[3*(i-1)]) + SQR(phi->elts[3*(i)+1] - phi->elts[3*(i-1)+1]) + SQR(phi->elts[3*(i)+2] - phi->elts[3*(i-1)+2])); dN++; } //if((1-(TphiPrime/(v->d[0])))<0.0) cout << (1-(TphiPrime/(v->d[0]))) << endl; } delete v; } } } //deletion delete a[2]; delete a[1]; delete a[0]; delete a; if(dN==0) { //return E/(((double) N)); return E/(((double) N) * len_fib(phi)); } else { //cout << N << " " << dN << endl; if(dN>=N) { //cout << "error N" << N << endl; return -1.0; } else { if(dLength<len_fib(phi)) { //return E/( (((double) N)-((double) dN)) ); return E/( (((double) N)-((double) dN)) * (len_fib(phi) - dLength)); } else { //cout << "returning -1.0: error length " << len_fib(phi) << " " << dLength << endl; //cout << len_fib(phi) << " " << dLength << endl; return -1.0; } } } #else //compute the length of the fiber //compute Reimann integrale (approximation) unsigned long N=0; double S = 0.0, phiX, phiY, phiZ, sumDelta=0.0, phiXdev, phiYdev, phiZdev, tmpS, tmpL; double** D = new double*[3]; D[0] = new double[3]; D[1] = new double[3]; D[2] = new double[3]; for(unsigned long i=0 ; i<(phi->N_element-1) ; i++) { //calculate speed vector phiX = phi->elts[3*(i+1)]-phi->elts[3*i]; phiY = phi->elts[3*(i+1)+1]-phi->elts[3*i+1]; phiZ = phi->elts[3*(i+1)+2]-phi->elts[3*i+2]; tmpL = sqrt(SQR(phiX) + SQR(phiY) + SQR(phiZ)); ///check norm of speed vector/// if(!(isnan(tmpL) || isinf(tmpL) || (tmpL<SMALL_NUM))) { //get tensor at point (phi->elts[3*i], phi->elts[3*i+2], phi->elts[3*i+2]) fillArray(phi->elts[3*i], phi->elts[3*i+1], phi->elts[3*i+2], D);//must check what is in D //calculates tensor product phiXdev = D[0][0]*phiX + D[0][1]*phiY + D[0][2]*phiZ; phiYdev = D[1][0]*phiX + D[1][1]*phiY + D[1][2]*phiZ; phiZdev = D[2][0]*phiX + D[2][1]*phiY + D[2][2]*phiZ; //calculates eigen values C_toolbox_eigen_sym v((double**) D, 3, false); tmpS = sqrt(SQR(phiXdev) + SQR(phiYdev) + SQR(phiZdev))/v.d[0]; if(!isnan(tmpS) && !isinf(tmpS)) { //integrate S += tmpS; //sum the length sumDelta +=tmpL; } else { cout << "tmpS nan or inf " << tmpS << endl; N++; } } else { cout << "tmpL nan or inf " << tmpL << endl; N++; } } //deletion delete D[2]; delete D[1]; delete D[0]; delete D; if(N<(phi->N_element-1)) return S/sumDelta; else return -1.0; #endif }
int main() { FILE * fin = NULL; char fn[MAX], input[MAX], word[MAX]; int choice = 0; int sentSize = 5; int aSize, nSize, vSize, pSize; char ** articles = NULL; char ** nouns = NULL; char ** verbs = NULL; char ** preps = NULL; char ** sentence = NULL; srand(time(NULL)); readFileName(fn); openFile(fn, fin, &aSize, &nSize, &vSize, &pSize); articles = fillArray(fn,"article", articles, aSize, fin); nouns = fillArray(fn,"noun", nouns, aSize, fin); verbs = fillArray(fn,"verb", verbs, aSize, fin); preps = fillArray(fn,"preposition", preps, aSize, fin); displayAmounts(aSize, nSize, vSize, pSize); sortStringArray(articles, aSize); sortStringArray(nouns, nSize); sortStringArray(verbs, vSize); sortStringArray(preps, pSize); do { choice = menu(); if(choice == 1) { sentence = createSentence(articles, nouns, verbs, preps, aSize, nSize, vSize, pSize); printArray2D(sentence,sentSize); }// end choice == 1 else if(choice == 2) { displayAllWords(articles, nouns, verbs, preps, aSize, nSize, vSize, pSize); }// end choice == 2 else if(choice == 3) { whichArray(input); whatWord(word); if(strcmp(input,"ARTICLES") == 0) addWord(articles, word, &aSize); else if(strcmp(input,"NOUNS") == 0) addWord(nouns, word, &nSize); else if(strcmp(input,"VERBS") == 0) addWord(verbs, word, &vSize); else if(strcmp(input,"PREPOSITIONS") == 0) addWord(preps, word, &pSize); }// end choice == 3 else if(choice == 4) { whichArray(input); whatWord(word); if(strcmp(input,"ARTICLES") == 0) removeWord(articles, word, &aSize); else if(strcmp(input,"NOUNS") == 0) removeWord(nouns, word, &nSize); else if(strcmp(input,"VERBS") == 0) removeWord(verbs, word, &vSize); else if(strcmp(input,"PREPOSITIONS") == 0) removeWord(preps, word, &pSize); }// end choice == 4 else if(choice == 5) { displayTogetherSorted(articles, nouns, verbs, preps, aSize, nSize, vSize, pSize); }// end choice == 5 }while(choice != 6); return 0; }
ReadData* BinTreeNodeReader::readString(int token) { if (token == -1) throw WAException("-1 token in readString", WAException::CORRUPT_STREAM_EX, -1); int bSize; ReadData *ret = new ReadData(); if (token > 2 && token <= 236) { if (token != 236) ret->data = new std::string(dictionary[token]); else { token = readInt8(this->in); ret->data = new std::string(extended_dict[token]); } ret->type = STRING; return ret; } switch (token) { case 0: delete ret; return NULL; case 252: bSize = readInt8(this->in); { std::vector<unsigned char>* buf8 = new std::vector<unsigned char>(bSize); fillArray(*buf8, bSize, this->in); ret->type = ARRAY; ret->data = buf8; } return ret; case 253: bSize = readInt24(this->in); { std::vector<unsigned char>* buf24 = new std::vector<unsigned char>(bSize); fillArray(*buf24, bSize, this->in); ret->type = ARRAY; ret->data = buf24; } return ret; case 255: bSize = readInt8(this->in); { int size = bSize & 0x7f; int numnibbles = size * 2 - ((bSize & 0x80) ? 1 : 0); std::vector<unsigned char> tmp(size); fillArray(tmp, size, this->in); std::string s; for (int i = 0; i < numnibbles; i++) { char c = (tmp[i / 2] >> (4 - ((i & 1) << 2))) & 0xF; if (c < 10) s += (c + '0'); else s += (c - 10 + '-'); } ret->type = STRING; ret->data = new std::string(s); } return ret; case 250: std::string* user = readStringAsString(); std::string* server = readStringAsString(); if ((user != NULL) && (server != NULL)) { std::string* result = new std::string(*user + "@" + *server); delete user; delete server; ret->type = STRING; ret->data = result; return ret; } if (server != NULL) { ret->type = STRING; ret->data = server; return ret; } throw WAException("readString couldn't reconstruct jid", WAException::CORRUPT_STREAM_EX, -1); } throw WAException("readString couldn't match token" + (int)token, WAException::CORRUPT_STREAM_EX, -1); }
int main(void) { int i; int j; int data; int data2=0; char in = 0; i = 0x0000000004000500; int numBad = 1; int count; long long cpi; volatile void *wptr; short leds = 0; char capInit = 0; //mv1kC1(0x9800000040000000, 0x9800000000001000); //setInterrupts(); //__writeStringLoopback("Stack TLB entry setup.\n"); __writeString("Hello World! Have a BERI nice day!\n"); //debugTlb(); //cp0Regs(); // causeTrap(); // __writeString("Came back from trap and still alive :)\n"); // sysCtrlTest(); // data = rdtscl(5); //int coreid = getCoreID(); /* if (coreid == 0) { delay(); coreCount++; } else { coreCount++; } */ while(in != 'Q') { if (in != '\n') { //if (coreid == 0) { globalReset = 0; coreFlag = 0; __writeString("\n Number of Cores in Use : "); __writeHex(coreCount); __writeString("\n Menu:\n"); __writeString(" \"F\" for floating point co-processor test.\n"); __writeString(" \"L\" for load-linked and store-conditional test.\n"); __writeString(" \"A\" for arithmetic test result.\n"); __writeString(" \"B\" array bounds checking benchmark.\n"); __writeString(" \"D\" for multiply and divide test.\n"); __writeString(" \"C\" for Count register test.\n"); __writeString(" \"M\" for eternal memory test.\n"); //__writeString(" \"N\" for networking test.\n"); __writeString(" \"V\" for framebuffer test.\n"); __writeString(" \"K\" for Capability initialization.\n"); __writeString(" \"l\" to invert LEDs.\n"); __writeString(" \"T\" for touchscreen test.\n"); __writeString(" \"q\" for quicksort boundschecking test.\n"); __writeString(" \"d\" for domain crossing benchmark.\n"); __writeString(" \"G\" for compositor test.\n"); __writeString(" \"Q\" to quit.\n"); } } in = __readUARTChar(); __writeUARTChar(in); __writeString("\n"); //__writeHex(in); //__writeString("\n"); if (in == 'F') { __writeString("Floating Point co-processor test\n"); CoProFPTest(); } if (in == 'L') { __writeString("Load-linked and store-conditional test:\n"); data = 13; data = ll(&data); data = sc(&data, 14); //__writeHex(data); __writeString(" < load-linked and store-conditional result (0)\n"); data = testNset(&data, 14); //__writeHex(data); __writeString(" < test and set result (1)\n"); } if (in == 'A') { __writeString("Arithmetic test:\n"); data = 0; data = arithmaticTest(); __writeHex(data); __writeString(" < arithmetic test result (0)\n"); } if (in == 'T') { int * tX= (int *)0x9000000005000000; int * tY= (int *)0x9000000005000004; int * tDown= (int *)0x9000000005000008; __writeString("X:"); data = *tX; __writeHex(data); __writeString(" Y:"); data = *tY; __writeHex(data); __writeString(" Down:"); data = *tDown; __writeHex(data); __writeString("\n"); } if (in == 'G') { __writeString("Compositor test:\n"); } if (in == 'D') { numBad = 1; __writeString("Multiply and divide test.\n"); for (i = -10; i < 10; i++) { data = numBad * i; __writeHex(numBad); __writeString(" * "); __writeHex(i); __writeString(" = "); __writeHex(data); __writeString("\n"); if (i!=0) data2 = data / i; __writeHex(data); __writeString(" / "); __writeHex(i); __writeString(" = "); __writeHex(data2); __writeString("\n"); __writeString("\n"); if (data == 0) data = 1; numBad = data; } } if (in == 'M') { __writeString("Memory test:\n"); i = 0; while(1) { count = getCount(); //__writeString("count:"); //__writeHex(count); //__writeString("\n"); int idx = 0; for (j=0; j<0x4000; j++) { idx = i+j; ((volatile int *)DRAM_BASE)[idx] = DRAM_BASE + (idx<<2); } for (j=0; j<0x4000; j++) { idx = i+j; data = ((volatile int *)DRAM_BASE)[idx]; if (data != (int)(DRAM_BASE + (idx<<2))) { //__writeHex((int)(DRAM_BASE + (idx<<2))); //__writeString(" = "); //__writeHex(data); //__writeString("?\n"); numBad++; } } cpi = getCount() - count; //__writeString("newCount - count:"); //__writeHex(cpi); //__writeString("\n"); __writeHex((int)(DRAM_BASE + (idx<<2))); __writeString(" = "); __writeHex(data); __writeString("?\n"); if (numBad == 0) { __writeString("All good! \n"); } else { __writeHex(numBad); __writeString(" were bad :(\n"); numBad = 0; } cpi = (cpi*1000); //__writeString("diff*1000:"); //__writeHex(cpi); //__writeString("\n"); // 8 instructions in the first loop, 12 in the second. cpi = cpi/((8+12)*0x4000); __writeString("CPI of "); __writeDigit(cpi); __writeString("\n"); i+=0x4000; if (i > 0x07000000) i = 0; } } if (in == 'C') { __writeString("Count Register Test:\n"); for(i=0;i<10;i++) { data = ((volatile int *)MIPS_PHYS_TO_UNCACHED(CHERI_COUNT))[0]; __writeHex(data); __writeString(", "); } __writeString("\n"); } if (in == 'K') { if (capInit==0) { FBIncBase(0x9000000004000000); long length = FBGetLeng(); length = length - 800*600*2; FBDecLeng(length); capInit = 1; } __writeString("C4.base= ");__writeHex(FBGetBase());__writeString("\n"); __writeString("C4.length= ");__writeHex(FBGetLeng());__writeString("\n"); CapRegDump(); } if (in == 'V') { int color = 0x8888; int x = 50; int y = 50; int length = 75; int height = 50; long frameBuff = 0x9000000004000000; for (x=200; x<500; x+=100) { for (y=300; y<500; y+=75) { draw3DRect(color, x, y, length, height); } } for (i=0; i<(800*600/4); i++) { FBSDR(0x0C63F80007E0001F,i<<3); } int offset = y*800 + x; int addOff; int totOff; for (i=0; i<(800*600); i++) { ((volatile short*)frameBuff)[i] = i; } for (i=0; i<height; i++) { for (j=0; j<length; j++) { addOff = (800*i) + j; totOff = (offset+addOff); ((volatile short*)frameBuff)[totOff] = color; } } } if (in == 'l') { leds = ~leds; IO_WR(CHERI_LEDS,leds); } if (in == 'N') { wptr = (void *)CHERI_NET_RX; i = *(volatile int *)wptr; __writeString("After accessing CHERI_NET_RX, read:\n"); __writeDigit(i); i = 0xabcd; wptr = (void *)CHERI_NET_TX; __writeString("Before writing 123 to CHERI_NET_TX\n"); *((volatile int *)CHERI_NET_TX) = i; __writeString("After writing 123 to CHERI_NET_TX\n"); } if (in == 'B') { arrayBench(); } if (in == 'q') { doQuicksort(); } if (in == 'd') { armArray(); } // ADDED >> if (in == 'X') { int A[SORT_SIZE]; writeString("Branch Exercise:\n"); fillArray(A, SORT_SIZE/2, 1000); bubbleSort(A, SORT_SIZE/2); writeString("Finished Bubble Sort!\n"); for (i = 0; i<SORT_SIZE/2; i+= SORT_SIZE/2/32) { writeHex(i); writeString(" = "); writeHex(A[i]); writeString("\n"); } fillArray(A, SORT_SIZE, 1234); quickSort(A, 0, SORT_SIZE); writeString("Finished Quick Sort!\n"); for (i = 0; i<SORT_SIZE; i+= SORT_SIZE/32) { writeHex(i); writeString(" = "); writeHex(A[i]); writeString("\n"); } writeString("Searching for each element...\n"); for (j = 0; j<4; j++) { for (i = 0; i<SORT_SIZE; i++) { binarySearch(A, A[i], 0, SORT_SIZE); } } writeString("Searching Done.\n"); writeString("Starting Modular Eponentiation\n"); for (i = 0; i<SORT_SIZE/4; i++) { writeHex(modExp(i,0xAAAAAAAAAAAAAAAA)); writeString("\n"); } } // ADDED << //debugRegs(); } return 0; }
void BinTreeNodeReader::fillBuffer(quint32 stanzaSize) { fillArray(readBuffer, stanzaSize); }
int main() { int loop = 0, loopMax = 5; double values[5][loopMax], increment[5] = {0,0,0,0,0}, averages[5] = {0,0,0,0,0}, spreads[5] = {0,0,0,0,0}; do{ long startTime, endTime; unsigned int x = arrayLength; int arrayA[x], arrayB[x], arrayC[x], arrayD[x], arrayE[x]; fillArray(arrayA, x, 1); fillArray(arrayB, x, 2); fillArray(arrayC, x, 3); fillArray(arrayD, x, 4); fillArray(arrayE, x, 5); /* printArray(arrayA, x); printf("\n\n"); printArray(arrayB, x); printf("\n\n"); printArray(arrayC, x); printf("\n\n"); printArray(arrayD, x); printf("\n\n"); printArray(arrayE, x); printf("\n\n"); system("pause"); */ printf("Starting QuickSort: Random\n"); startTime = GetTickCount(); quickSort(arrayA, 0, x); endTime = GetTickCount(); increment[0] += (double)(endTime-startTime)/1000; values[0][loop] = (double)(endTime-startTime)/1000; printf("Done! Took %.2f s\n\n", (double)(endTime-startTime)/1000); printf("Starting QuickSort: In order\n"); startTime = GetTickCount(); quickSort(arrayB, 0, x); endTime = GetTickCount(); increment[1] += (double)(endTime-startTime)/1000; values[1][loop] = (double)(endTime-startTime)/1000; printf("Done! Took %.2f s\n\n", (double)(endTime-startTime)/1000); printf("Starting QuickSort: Reversed\n"); startTime = GetTickCount(); quickSort(arrayC, 0, x); endTime = GetTickCount(); increment[2] += (double)(endTime-startTime)/1000; values[2][loop] = (double)(endTime-startTime)/1000; printf("Done! Took %.2f s\n\n", (double)(endTime-startTime)/1000); printf("Starting QuickSort: 1,100,2,99...\n"); startTime = GetTickCount(); quickSort(arrayD, 0, x); endTime = GetTickCount(); increment[3] += (double)(endTime-startTime)/1000; values[3][loop] = (double)(endTime-startTime)/1000; printf("Done! Took %.2f s\n\n", (double)(endTime-startTime)/1000); printf("Starting QuickSort: 100,1,99,2\n"); startTime = GetTickCount(); quickSort(arrayE, 0, x); endTime = GetTickCount(); increment[4] += (double)(endTime-startTime)/1000; values[4][loop] = (double)(endTime-startTime)/1000; printf("Done! Took %.2f s\n\n", (double)(endTime-startTime)/1000); printf("Checking arrays...\n\n"); //prints printf("\n\nChecking QuickSort: Random\n"); checkArray(arrayA, x); printf("\n\nChecking QuickSort: Sorted\n"); checkArray(arrayB, x); printf("\n\nChecking QuickSort: Reversed\n\n"); checkArray(arrayC, x); printf("\n\nChecking QuickSort: 1,100,2,99...\n"); checkArray(arrayD, x); printf("\n\nChecking QuickSort: 100,1,99,2\n"); checkArray(arrayE, x); printf("\n\n"); loop++; }while(loop < loopMax); int i; for(i = 0; i < 5;i++){ averages[i] = increment[i]/loopMax; } for(i = 0; i < 5;i++){ spreads[i] = calculateSpread(values[i], loopMax, i, averages); } printf("Random \n"); printf("Average: %.2f \n", averages[0]); printf("Spread: %.2lf \n\n", spreads[0]); printf("Sorted \n"); printf("Average: %.2f \n", averages[1]); printf("Spread: %.2lf \n\n", spreads[1]); printf("Reversed \n"); printf("Average: %.2f \n", averages[2]); printf("Spread: %.2lf \n\n", spreads[2]); printf("1,100,2,99... \n"); printf("Average: %.2f \n", averages[3]); printf("Spread: %.2lf \n\n", spreads[3]); printf("100,1,99,2... \n"); printf("Average: %.2f \n", averages[4]); printf("Spread: %.2lf \n\n", spreads[4]); system("PAUSE"); return 0; }
int main(int argc, const char** argv) { cl_uint platform_count; cl_platform_id platforms[5]; cl_int err = CL_SUCCESS; unsigned int i, p; cl_device_type dev_type = CL_DEVICE_TYPE_ALL; void * ptrs[BLOCKS]; cl_command_queue cqs[BLOCKS]; cl_mem d_A[BLOCKS]; cl_mem d_C[BLOCKS]; cl_mem d_B[BLOCKS]; cl_event GPUDone[BLOCKS]; cl_event GPUExecution[BLOCKS]; struct timeval start, end; int workOffset[BLOCKS]; int workSize[BLOCKS]; unsigned int sizePerGPU = HC / BLOCKS; unsigned int sizeMod = HC % BLOCKS; size_t A_size = WA * HA; size_t A_mem_size = sizeof(TYPE) * A_size; TYPE* A_data; size_t B_size = WB * HB; size_t B_mem_size = sizeof(TYPE) * B_size; TYPE* B_data; size_t C_size = WC * HC; size_t C_mem_size = sizeof(TYPE) * C_size; TYPE* C_data; parse_args(argc, argv); check(clGetPlatformIDs(5, platforms, &platform_count)); if (platform_count == 0) { printf("No platform found\n"); exit(77); } cl_uint device_count; cl_uint devs[platform_count]; cl_device_id * devices[platform_count]; cl_context ctx[platform_count]; cl_command_queue * commandQueue[platform_count]; device_count = 0; for (p=0; p<platform_count; p++) { cl_platform_id platform = platforms[p]; err = clGetDeviceIDs(platform, dev_type, 0, NULL, &devs[p]); if (err == CL_DEVICE_NOT_FOUND) { devs[p] = 0; continue; } if (devs[p] == 0) { printf("No OpenCL device found\n"); exit(77); } if (err != CL_SUCCESS) { fprintf(stderr, "OpenCL Error (%d) in clGetDeviceIDs()\n", err); exit(EXIT_FAILURE); } if (devs[p] == 0) continue; devices[p] = (cl_device_id*)malloc(sizeof(cl_device_id) * devs[p]); commandQueue[p] = (cl_command_queue*)malloc(sizeof(cl_command_queue) * devs[p]); check(clGetDeviceIDs(platform, dev_type, devs[p], devices[p], NULL)); cl_context_properties properties[] = {CL_CONTEXT_PLATFORM, (cl_context_properties)platform, 0}; check2(ctx[p] = clCreateContext(properties, devs[p], devices[p], NULL, NULL, &err)); for(i = 0; i < devs[p]; ++i) { cl_device_id device = devices[p][i]; char name[2048]; name[0] = '\0'; clGetDeviceInfo(device, CL_DEVICE_NAME, 2048, name, NULL); printf("Device %d: %s\n", i, name); check2(commandQueue[p][i] = clCreateCommandQueue(ctx[p], device, CL_QUEUE_PROFILING_ENABLE | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err)); } device_count += devs[p]; } if (device_count == 0) error("No device found\n"); cl_kernel multiplicationKernel[platform_count]; printf("\nUsing Matrix Sizes: A(%lu x %lu), B(%lu x %lu), C(%lu x %lu)\n", (unsigned long)WA, (unsigned long)HA, (unsigned long)WB, (unsigned long)HB, (unsigned long)WC, (unsigned long)HC); // allocate host memory for matrices A, B and C A_data = (TYPE*)malloc(A_mem_size); if (A_data == NULL) { perror("malloc"); } B_data = (TYPE*)malloc(B_mem_size); if (B_data == NULL) { perror("malloc"); } C_data = (TYPE*) malloc(C_mem_size); if (C_data == NULL) { perror("malloc"); } cl_program program[platform_count]; for (p=0; p<platform_count; p++) { if (devs[p] == 0) continue; check2(program[p] = clCreateProgramWithSource(ctx[p], 1, (const char **)&code, NULL, &err)); check(clBuildProgram(program[p], 0, NULL, NULL, NULL, NULL)); check2(multiplicationKernel[p] = clCreateKernel(program[p], "sgemmNN", &err)); } printf("Initializing data...\n"); srand(2008); fillArray(A_data, A_size); fillArray(B_data, B_size); memset(C_data, 0, C_size); printf("Computing...\n"); workOffset[0] = 0; gettimeofday(&start, NULL); size_t localWorkSize[] = {BLOCK_SIZE, BLOCK_SIZE}; int c = 0; for (p=0; p<platform_count;p++) { for (i=0; i<devs[p]; i++) { check2(d_B[c] = clCreateBuffer(ctx[p], CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, HB * WB * sizeof(TYPE), B_data, &err)); c++; } } for(i=0; i < BLOCKS; ++i) { int d = i % device_count; cl_uint platform = 0; // determine device platform int dev = d; for (platform = 0; platform < platform_count; platform++) { if ((cl_int)(dev - devs[platform]) < 0) break; dev -= devs[platform]; } workSize[i] = (i < sizeMod) ? sizePerGPU+1 : sizePerGPU; check2(d_A[i] = clCreateBuffer(ctx[platform], CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, workSize[i] * WA * sizeof(TYPE), &A_data[workOffset[i] * WA], &err)); check2(d_C[i] = clCreateBuffer(ctx[platform], CL_MEM_WRITE_ONLY | CL_MEM_USE_HOST_PTR, workSize[i] * WC * sizeof(TYPE), &C_data[workOffset[i] * WC], &err)); check(clSetKernelArg(multiplicationKernel[platform], 0, sizeof(cl_int), &workSize[i])); check(clSetKernelArg(multiplicationKernel[platform], 1, sizeof(cl_int), &workSize[i])); check(clSetKernelArg(multiplicationKernel[platform], 2, sizeof(cl_int), &workSize[i])); check(clSetKernelArg(multiplicationKernel[platform], 3, sizeof(cl_mem), (void *) &d_A[i])); check(clSetKernelArg(multiplicationKernel[platform], 4, sizeof(cl_mem), (void *) &d_B[d])); check(clSetKernelArg(multiplicationKernel[platform], 5, sizeof(cl_mem), (void *) &d_C[i])); size_t globalWorkSize[] = {roundUp(BLOCK_SIZE,WC), roundUp(BLOCK_SIZE,workSize[i])}; check(clEnqueueNDRangeKernel(commandQueue[platform][dev], multiplicationKernel[platform], 2, NULL, globalWorkSize, localWorkSize, 0, NULL, &GPUExecution[i])); // Non-blocking copy of result from device to host cqs[i] = commandQueue[platform][dev]; check2(ptrs[i] = clEnqueueMapBuffer(cqs[i], d_C[i], CL_FALSE, CL_MAP_READ, 0, WC * sizeof(TYPE) * workSize[i], 1, &GPUExecution[i], &GPUDone[i], &err)); if(i+1 < BLOCKS) workOffset[i + 1] = workOffset[i] + workSize[i]; } // CPU sync with GPU for (p=0; p<platform_count;p++) { cl_uint dev; for (dev=0; dev<devs[p]; dev++) { clFinish(commandQueue[p][dev]); } } gettimeofday(&end, NULL); double timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec)); double dSeconds = timing/1000/1000; double dNumOps = 2.0 * (double)WA * (double)HA * (double)WB; double gflops = 1.0e-9 * dNumOps/dSeconds; printf("Throughput = %.4f GFlops/s, Time = %.5f s, Size = %.0f, NumDevsUsed = %d, Blocks = %ld, Workgroup = %zu\n", gflops, dSeconds, dNumOps, device_count, BLOCKS, localWorkSize[0] * localWorkSize[1]); // compute reference solution if (check) { printf("Comparing results with CPU computation... "); TYPE* reference = (TYPE*)malloc(C_mem_size); computeReference(reference, A_data, B_data, HA, WA, WB); // check result int res = shrCompareL2fe(reference, C_data, C_size, 1.0e-6f); if (res == 0) { printf("\n\n"); printDiff(reference, C_data, WC, HC, 100, 1.0e-5f); } else printf("PASSED\n\n"); free(reference); } for(i = 0; i < BLOCKS; i++) { clEnqueueUnmapMemObject(cqs[i], d_C[i], ptrs[i], 0, NULL, NULL); } for(i = 0; i < BLOCKS; i++) { clFinish(cqs[i]); } for (i=0; i<device_count; i++) { clReleaseMemObject(d_B[i]); } for(i = 0; i < BLOCKS; i++) { clReleaseMemObject(d_A[i]); clReleaseMemObject(d_C[i]); clReleaseEvent(GPUExecution[i]); clReleaseEvent(GPUDone[i]); } for (p=0; p<platform_count;p++) { if (devs[p] == 0) continue; check(clReleaseKernel(multiplicationKernel[p])); check(clReleaseProgram(program[p])); check(clReleaseContext(ctx[p])); cl_uint k; for(k = 0; k < devs[p]; ++k) { check(clReleaseCommandQueue(commandQueue[p][k])); } } free(A_data); free(B_data); free(C_data); return 0; }