rb_node* rb_insert(uint32_t key, rb_tree* tree) { rb_node* n = calloc(1,sizeof(rb_node)); n->key = key; n->color = RED; n->left = tree->nil; n->right = tree->nil; #ifdef DEBUG is_correct(tree); #endif if(tree->root) tree_insert(n, tree->root, tree); else tree->root = n; #ifdef DEBUG is_correct(tree); #endif insert_case1(n, tree); #ifdef DEBUG is_correct(tree); #endif tree->n++; return n; }
void rotate_left(rb_node* pivot, rb_tree* tree) { #ifdef DEBUG is_correct(tree); assert(!is_leaf(pivot->right, tree)); #endif rb_node* root = pivot->right; rb_node* move = root->left; root->parent = pivot->parent; pivot->right = move; if(!is_leaf(move, tree)) move->parent = pivot; root->left = pivot; pivot->parent = root; if(root->parent) { if (pivot == root->parent->left) root->parent->left = root; else root->parent->right = root; } else { tree->root = root; } #ifdef DEBUG is_correct(tree); #endif }
int main() { int T, i; char input1[8]; char input2[8]; char submit_result[10]; char op; int answer; struct num_info_s num_info[11]; fscanf(stdin, "%d", &T); for (i = 0; i < T; i++) { init_num_info(num_info); fscanf(stdin, "%s %c %s = %s", input1, &op, input2, submit_result); answer = calculate(input1, input2, op); if (answer < 0 || answer >10) fprintf(stdout, "No\n"); else if (!is_correct(answer, submit_result, num_info)) fprintf(stdout, "No\n"); else fprintf(stdout, "Yes\n"); } return 0; }
union_shape::union_shape(std::initializer_list<std::unique_ptr<shape>> shapes) { for (auto& ptr : shapes_) { auto b (ptr->bounding_box()); if (b.is_correct()) bbox_ = bbox_ + b; } }
// --------------------------------------------------------------------- // check_correctness // --------------------------------------------------------------------- void check_correctness(rlist* in, rlist* out) { #if CHECK_CORRECTNESS int res = is_correct(in, out); #if VERBOSE printf("correctness test: %s\n", res ? "[passed]" : ">> [failed] <<"); #endif if (!res) exit(1); #endif }
std::vector<placement_problem> placement_problem::branch(branching_rule rule) const{ // Chose a good branch based simply on the positions of the cells std::vector<point> pos = get_positions(); int best_fc, best_sc; int best_cell_measure=-1; bool found_cell_overlap=false; // Branch to avoid overlaps between cells for(int i=0; i+1<cells.size(); ++i){ for(int j=i+1; j<cells.size(); ++j){ int measure = evaluate_branch(i, j, pos, rule); if(measure >= 0 and measure > best_cell_measure){ found_cell_overlap=true; best_cell_measure = measure; best_fc = i; best_sc = j; } } } rect best_fixed; int best_fixed_c; int best_fixed_measure=-1; bool found_fixed_overlap=false; for(rect const R : fixed_elts){ for(int i=0; i<cells.size(); ++i){ int measure = evaluate_branch(i, R, pos, rule); if(measure >= 0 and measure > best_fixed_measure){ found_fixed_overlap=true; best_fixed_measure = measure; best_fixed = R; best_fixed_c = i; } } } if(found_cell_overlap and (not found_fixed_overlap or best_cell_measure >= best_fixed_measure) ){ return branch_overlap_removal(best_fc, best_sc); } else if(found_fixed_overlap){ return branch_overlap_removal(best_fixed_c, best_fixed); } else{ assert(is_correct()); return std::vector<placement_problem>(); } }
// solve field bool solve(field_ptr field) { int row, col; if (!find_unassigned(field, row, col)) return true; for (int n = 1; n <= 9; n++) { if (is_correct(field, row, col, n)) { field[row][col] = n; if (solve(field)) return true; field[row][col] = UNASSIGNED; } } return false; }
void process(char str[]) { int i, j; bool res; int len = strlen(str); if(is_correct(str, len)) { printf("%s is correct\n", str); return; } else { printf("%s:", str); for(i = 0; i < n; i++) { if(len == words[i].len) { res = false; for(j = 0; j < len; j++) { if(str[j] != words[i].w[j]) { if(!res) res = true; else break; } } if(j == len) { printf(" %s", words[i].w); } } else if(abs(len - words[i].len) == 1) { if(len < words[i].len) res = str_cmp(str, len, words[i].w, words[i].len); else res = str_cmp(words[i].w, words[i].len, str, len); if(res) printf(" %s", words[i].w); } } printf("\n"); } }
/** **/ int main (int argc, char* argv[]) { int WORK_DIM = 2; // Wie viele Dimensionen hat der Indexraum? std::chrono::time_point<std::chrono::system_clock> s_start, s_end, p_start, p_end; // Lese den Kernel dynamisch ein: (uebernommen von Foliensatz 9, Folie 20) FILE *fp; const char *FileName = "matmult.cl"; char *KernelSource; fp = fopen(FileName, "r"); if (!fp) { printf("Can't open kernel source: %s", FileName); exit(1); } KernelSource = (char *)malloc(MAX_SOURCE_SIZE); size_t kernel_s_size = fread(KernelSource, 1, MAX_SOURCE_SIZE, fp); fclose(fp); cl_int err; cl_platform_id* platforms = NULL; char platform_name[1024]; cl_device_id device_id = NULL; cl_uint num_of_platforms = 0, num_of_devices = 0; cl_context context; cl_kernel kernel; cl_command_queue command_queue; cl_program program; err = clGetPlatformIDs(0, NULL, &num_of_platforms); if (err != CL_SUCCESS) { printf("No platforms found. Error: %d\n", err); return 0; } // Liefert Plattformen platforms = (cl_platform_id *)malloc(num_of_platforms); err = clGetPlatformIDs(num_of_platforms, platforms, NULL); if (err != CL_SUCCESS) { printf("No platforms found. Error: %d\n", err); return 0; } else { int nvidia_platform = 0; // Speichert den Rang der letzten NVIDIA-Plattform for (unsigned int i=0; i<num_of_platforms; i++) // Fuer jede Plattform: { clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, sizeof(platform_name), platform_name, NULL); if (err != CL_SUCCESS) { printf("Could not get information about platform. Error: %d\n", err); return 0; } if (strstr(platform_name, "NVIDIA") != NULL) { // Falls die Plattform eine NVIDIA-Plattform ist: Speichere ihren Rang nvidia_platform = i; break; } } // Gibt die ID des Devices der NVIDIA-Plattform zurueck err = clGetDeviceIDs(platforms[nvidia_platform], CL_DEVICE_TYPE_GPU, 1, &device_id, &num_of_devices); if (err != CL_SUCCESS) { printf("Could not get device in platform. Error: %d\n", err); return 0; } } // Erschaffe einen OpenCl-context, in dem OpenCl-Datenobjekte verwaltet werden koennen context = clCreateContext(0, 1, &device_id, NULL, NULL, &err); if (err != CL_SUCCESS) { printf("Unable to create context. Error: %d\n", err); return 0; } // Initialisiere eine Befehlswarteschleife, die Befehle fuer OpenCl-Objekte speichern kann command_queue = clCreateCommandQueue(context, device_id, 0, &err); if (err != CL_SUCCESS) { printf("Unable to create command queue. Error: %d\n", err); return 0; } // Initialisiere ein Programm und spezifiziere, aus welchem Code dieses kompiliert werden soll program = clCreateProgramWithSource(context, 1, (const char **)&KernelSource, (const size_t *)& kernel_s_size, &err); if (err != CL_SUCCESS) { printf("Unable to create program. Error: %d\n", err); return 0; } // Kompiliere das Programm zur Laufzeit err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (err != CL_SUCCESS) { // Zeige Compilermeldungen an: (uebernommen von Foliensatz 9, Folie 23) char *log; size_t size; clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &size); log = (char *)malloc(size+1); if (log) { clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, size, log, NULL); log[size] = '\0'; printf("%s", log); free(log); } printf("Error building program. Error: %d\n", err); return 0; } // Erschaffe einen Kernel und lade oben kompiliertes Programm ein kernel = clCreateKernel(program, "matmult", &err); if (err != CL_SUCCESS) { printf("Error setting kernel. Error: %d\n", err); return 0; } float **A, **B, **C; // Matrizen int dim1, dim2, dim3; // Matrixdimensionen dim1 = D1; // Zeilen von A, Zeilen von C dim2 = D2; // Spalten von A, Zeilen von B dim3 = D3; // Spalten von B, Spalten von C A = alloc_mat(dim1, dim2); B = alloc_mat(dim2, dim3); C = alloc_mat(dim1, dim3); init_mat(A, dim1, dim2); init_mat(B, dim2, dim3); cl_mem in_A, in_B, output; // float data[DATA_SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; size_t global[1] = {dim1*dim3}; // Dimensionen von C size_t global_two[2] = {dim1, dim3}; in_A = clCreateBuffer (context, CL_MEM_READ_ONLY, sizeof(float)*dim1*dim2, NULL, &err); in_B = clCreateBuffer (context, CL_MEM_READ_ONLY, sizeof(float)*dim2*dim3, NULL, &err); output = clCreateBuffer (context, CL_MEM_WRITE_ONLY, sizeof(float)*dim1*dim3, NULL, &err); clEnqueueWriteBuffer(command_queue, in_A, CL_TRUE, 0, sizeof(float)*dim1*dim2, *A, 0, NULL, NULL); clEnqueueWriteBuffer(command_queue, in_B, CL_TRUE, 0, sizeof(float)*dim2*dim3, *B, 0, NULL, NULL); clSetKernelArg(kernel, 0, sizeof(cl_mem), &in_A); clSetKernelArg(kernel, 1, sizeof(cl_mem), &in_B); clSetKernelArg(kernel, 2, sizeof(cl_mem), &output); // clSetKernelArg(kernel, 3, sizeof(int), &dim2); // clSetKernelArg(kernel, 4, sizeof(int), &dim3); clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, global, NULL, 0, NULL, NULL); if (WORK_DIM == 2) { clEnqueueNDRangeKernel(command_queue, kernel, 2, NULL, global_two, NULL, 0, NULL, NULL); } // Zeitmessung fuer parallele Version p_start = std::chrono::system_clock::now(); err = clFinish(command_queue); p_end = std::chrono::system_clock::now(); std::chrono::duration<double> p_duration = p_end - p_start; if (err == CL_INVALID_COMMAND_QUEUE ) { printf("CL_INVALID_COMMAND_QUEUE: %d\n", err); return 0; } clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0, sizeof(float)*dim1*dim3, *C, 0, NULL, NULL); // Ueberpruefe, ob serielle Version und parallele gleich sind: float **correct_matrix; correct_matrix = alloc_mat(dim1, dim3); s_start = std::chrono::system_clock::now(); // Zeitmessung fuer serielle Version correct_matrix = mult_mat(A, B, dim1, dim2, dim3); s_end = std::chrono::system_clock::now(); std::chrono::duration<double> s_duration = s_end - s_start; is_correct(C, correct_matrix, dim1, dim3); // Numerischer Korrektheitsbeweis print_mat(C, dim1, dim3, "C = "); print_mat(correct_matrix, dim1, dim3, "correct_matrix = "); // printf("Kernel execution time: %f\n", t_end-t_start); clReleaseMemObject(in_A); clReleaseMemObject(in_B); clReleaseMemObject(output); clReleaseProgram(program); clReleaseKernel(kernel); err = clReleaseCommandQueue(command_queue); //!! if (err != CL_SUCCESS) { printf("Error releasing command queue: %d\n", err); return 0; } clReleaseContext(context); printf("Dauer der seriellen Version: %.2f Millisekunden\n", s_duration.count() * 1000); printf("Dauer der parallelen Version: %.2f Millisekunden\n", p_duration.count() * 1000); printf("Erhaltenes Speed Up: %.2f \n", p_duration.count() / p_duration.count()); return 0; }
void MainWindow::changeMoneyDiff() { QVector<bool> is_correct(MONEY_EDIT_FIELDS_COUNT /*= 12*/, true); QVector<float> new_values(MONEY_EDIT_FIELDS_COUNT + MONEY_DIFF_FIELDS_COUNT, 0.0f); new_values[0] = ui_->CurRubNalEdit->text().toFloat(&is_correct[0]); new_values[1] = ui_->CurEurNalEdit->text().toFloat(&is_correct[1]); new_values[2] = ui_->CurUsdNalEdit->text().toFloat(&is_correct[2]); new_values[3] = ui_->CurRubElecEdit->text().toFloat(&is_correct[3]); new_values[4] = ui_->CurEurElecEdit->text().toFloat(&is_correct[4]); new_values[5] = ui_->CurUsdElecEdit->text().toFloat(&is_correct[5]); new_values[6] = ui_->OldRubNalEdit->text().toFloat(&is_correct[6]); new_values[7] = ui_->OldEurNalEdit->text().toFloat(&is_correct[7]); new_values[8] = ui_->OldUsdNalEdit->text().toFloat(&is_correct[8]); new_values[9] = ui_->OldRubElecEdit->text().toFloat(&is_correct[9]); new_values[10] = ui_->OldEurElecEdit->text().toFloat(&is_correct[10]); new_values[11] = ui_->OldUsdElecEdit->text().toFloat(&is_correct[11]); bool passed = true; for (bool elem : is_correct) passed &= elem; if (passed) { money_.setCurRubNalValue(new_values[0]); money_.setCurEurNalValue(new_values[1]); money_.setCurUsdNalValue(new_values[2]); money_.setCurRubElecValue(new_values[3]); money_.setCurEurElecValue(new_values[4]); money_.setCurUsdElecValue(new_values[5]); money_.setOldRubNalValue(new_values[6]); money_.setOldEurNalValue(new_values[7]); money_.setOldUsdNalValue(new_values[8]); money_.setOldRubElecValue(new_values[9]); money_.setOldEurElecValue(new_values[10]); money_.setOldUsdElecValue(new_values[11]); } QString value_str; ui_->CurRubNalEdit->setText(value_str.setNum(money_.getCurRubNalValue())); ui_->CurEurNalEdit->setText(value_str.setNum(money_.getCurEurNalValue())); ui_->CurUsdNalEdit->setText(value_str.setNum(money_.getCurUsdNalValue())); ui_->CurRubElecEdit->setText(value_str.setNum(money_.getCurRubElecValue())); ui_->CurEurElecEdit->setText(value_str.setNum(money_.getCurEurElecValue())); ui_->CurUsdElecEdit->setText(value_str.setNum(money_.getCurUsdElecValue())); ui_->OldRubNalEdit->setText(value_str.setNum(money_.getOldRubNalValue())); ui_->OldEurNalEdit->setText(value_str.setNum(money_.getOldEurNalValue())); ui_->OldUsdNalEdit->setText(value_str.setNum(money_.getOldUsdNalValue())); ui_->OldRubElecEdit->setText(value_str.setNum(money_.getOldRubElecValue())); ui_->OldEurElecEdit->setText(value_str.setNum(money_.getOldEurElecValue())); ui_->OldUsdElecEdit->setText(value_str.setNum(money_.getOldUsdElecValue())); ui_->RubDiffEdit->setText(value_str.setNum(money_.getRubDiffValue())); ui_->EurDiffEdit->setText(value_str.setNum(money_.getEurDiffValue())); ui_->UsdDiffEdit->setText(value_str.setNum(money_.getUsdDiffValue())); setQLineEditColor(ui_->RubDiffEdit, money_.getRubDiffValue()); setQLineEditColor(ui_->EurDiffEdit, money_.getEurDiffValue()); setQLineEditColor(ui_->UsdDiffEdit, money_.getUsdDiffValue()); }
int main(int argc, char** argv) { double serial_time, openCL_time, start_time; cl_int err; cl_platform_id* platforms = NULL; char platform_name[1024]; cl_device_id device_id = NULL; cl_uint num_of_platforms = 0; cl_uint num_of_devices = 0; cl_context context; cl_kernel kernel; cl_command_queue command_queue; cl_program program; cl_mem input1, input2, input3, output; float **A, **B, **C, **serialC; // matrices int d1, d2, d3; // dimensions of matrices /* print user instruction */ if (argc != 4) { printf("Matrix multiplication: C = A x B\n"); printf("Usage: %s <NumRowA> <NumColA> <NumColB>\n", argv[0]); return 0; } /* read user input */ d1 = 1000; // rows of A and C d2 = 1000; // cols of A and rows of B d3 = 1000; // cols of B and C int d[4] = { 0, d1, d2, d3 }; size_t global[1] = { (size_t)d1*d3 }; printf("Matrix sizes C[%d][%d] = A[%d][%d] x B[%d][%d]\n", d1, d3, d1, d2, d2, d3); /* prepare matrices */ A = alloc_mat(d1, d2); init_mat(A, d1, d2); B = alloc_mat(d2, d3); init_mat(B, d2, d3); C = alloc_mat(d1, d3); serialC = alloc_mat(d1, d3); err = clGetPlatformIDs(0, NULL, &num_of_platforms); if (err != CL_SUCCESS) { printf("No platforms found. Error: %d\n", err); return 0; } platforms = (cl_platform_id *)malloc(num_of_platforms); err = clGetPlatformIDs(num_of_platforms, platforms, NULL); if (err != CL_SUCCESS) { printf("No platforms found. Error: %d\n", err); return 0; } else { int nvidia_platform = 0; for (unsigned int i = 0; i<num_of_platforms; i++) { clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, sizeof(platform_name), platform_name, NULL); if (err != CL_SUCCESS) { printf("Could not get information about platform. Error: %d\n", err); return 0; } if (strstr(platform_name, "NVIDIA") != NULL) { nvidia_platform = i; break; } } err = clGetDeviceIDs(platforms[nvidia_platform], CL_DEVICE_TYPE_GPU, 1, &device_id, &num_of_devices); if (err != CL_SUCCESS) { printf("Could not get device in platform. Error: %d\n", err); return 0; } } context = clCreateContext(0, 1, &device_id, NULL, NULL, &err); if (err != CL_SUCCESS) { printf("Unable to create context. Error: %d\n", err); return 0; } command_queue = clCreateCommandQueue(context, device_id, 0, &err); if (err != CL_SUCCESS) { printf("Unable to create command queue. Error: %d\n", err); return 0; } program = clCreateProgramWithSource(context, 1, (const char **)&KernelSource, NULL, &err); if (err != CL_SUCCESS) { printf("Unable to create program. Error: %d\n", err); return 0; } if (clBuildProgram(program, 0, NULL, NULL, NULL, NULL) != CL_SUCCESS) { char *log; size_t size; clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL, &size); // 1. Länge des Logbuches? log = (char *)malloc(size + 1); if (log) { clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, size, log, NULL); // 2. Hole das Logbuch ab log[size] = '\0'; printf("%s", log); free(log); } return 1; } err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (err != CL_SUCCESS) { printf("Error building program. Error: %d\n", err); return 0; } kernel = clCreateKernel(program, "matmult_ocl", &err); if (err != CL_SUCCESS) { printf("Error setting kernel. Error: %d\n", err); return 0; } input1 = clCreateBuffer(context, CL_MEM_READ_ONLY, d1*d2*sizeof(float), NULL, &err); input2 = clCreateBuffer(context, CL_MEM_READ_ONLY, d2*d3*sizeof(float), NULL, &err); input3 = clCreateBuffer(context, CL_MEM_READ_ONLY, 4 * sizeof(int), NULL, &err); output = clCreateBuffer(context, CL_MEM_READ_WRITE, d1*d3*sizeof(float), NULL, &err); start_time = omp_get_wtime(); clEnqueueWriteBuffer(command_queue, input1, CL_TRUE, 0, d1*d2*sizeof(float), *A, 0, NULL, NULL); clEnqueueWriteBuffer(command_queue, input2, CL_TRUE, 0, d2*d3*sizeof(float), *B, 0, NULL, NULL); clEnqueueWriteBuffer(command_queue, input3, CL_TRUE, 0, 4 * sizeof(int), d, 0, NULL, NULL); clSetKernelArg(kernel, 0, sizeof(cl_mem), &input1); clSetKernelArg(kernel, 1, sizeof(cl_mem), &input2); clSetKernelArg(kernel, 2, sizeof(cl_mem), &input3); clSetKernelArg(kernel, 3, sizeof(cl_mem), &output); clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, global, NULL, 0, NULL, NULL); clFinish(command_queue); clEnqueueReadBuffer(command_queue, output, CL_TRUE, 0, d1*d3*sizeof(float), *C, 0, NULL, NULL); // for (unsigned int i = 0; i < (unsigned int) d1*d3; i++) // printf("%f\n", C[0][i]); openCL_time = omp_get_wtime() - start_time; clReleaseMemObject(input1); clReleaseMemObject(input2); clReleaseMemObject(input3); clReleaseMemObject(output); clReleaseProgram(program); clReleaseKernel(kernel); clReleaseCommandQueue(command_queue); clReleaseContext(context); printf("Running serial algorithm...\n"); start_time = omp_get_wtime(); serialC = mult_mat(A, B, d1, d2, d3); serial_time = omp_get_wtime() - start_time; printf("Checking results... "); is_correct(C, serialC, d1, d3); printf("Showing stats...\n"); printf(" serial runtime = %f\n", serial_time); printf(" OpenCL runtime = %f\n", openCL_time); printf(" Speedup = %f\n", serial_time / openCL_time); return 0; }