void initAligned() { weightsAligned.reset(connsCount); srcConnsAligned.reset(connsCount); tgtConnsAligned.reset(connsCount); for(int i = 0; i < connsCount; i++) { weightsAligned[i] = weights[i]; srcConnsAligned[i] = srcConns[i]; tgtConnsAligned[i] = tgtConns[i]; } endConnsAligned.reset(layersCount); endNodesAligned.reset(layersCount); for(int i = 0; i < layersCount; i++) { endConnsAligned[i] = endConns[i]; endNodesAligned[i] = endNodes[i]; } outputsAligned.reset(outputsCount); for(int i = 0; i < outputsCount; i++) { outputsAligned[i] = 0; } outputIdsAligned.reset(outputIds.size()); for(int i = 0; i < outputIds.size(); i++) { outputIdsAligned[i] = outputIds[i]; } }
// Free the resources allocated during initialization void cleanup() { for(unsigned i = 0; i < num_devices; ++i) { if(kernel && kernel[i]) { clReleaseKernel(kernel[i]); } if(queue && queue[i]) { clReleaseCommandQueue(queue[i]); } #if USE_SVM_API == 0 if(input_a_buf && input_a_buf[i]) { clReleaseMemObject(input_a_buf[i]); } if(input_b_buf && input_b_buf[i]) { clReleaseMemObject(input_b_buf[i]); } if(output_buf && output_buf[i]) { clReleaseMemObject(output_buf[i]); } #else if(input_a[i].get()) input_a[i].reset(); if(output[i].get()) output[i].reset(); #endif /* USE_SVM_API == 0 */ } #if USE_SVM_API == 1 if(input_b.get()) input_b.reset(); #endif /* USE_SVM_API == 1 */ if(program) { clReleaseProgram(program); } if(context) { clReleaseContext(context); } }
// Initialize the data for the problem. Requires num_devices to be known. void init_problem() { if(num_devices == 0) { checkError(-1, "No devices"); } // Generate input matrices A and B. For matrix A, we divide up the host // buffers so that the buffers are aligned for each device. The whole of // matrix B is used by each device, so it does not need to be divided. printf("Generating input matrices\n"); input_a.reset(num_devices); output.reset(num_devices); #if USE_SVM_API == 0 for(unsigned i = 0; i < num_devices; ++i) { input_a[i].reset(rows_per_device[i] * A_width); output[i].reset(rows_per_device[i] * C_width); for(unsigned j = 0; j < rows_per_device[i] * A_width; ++j) { input_a[i][j] = rand_float(); } } input_b.reset(B_height * B_width); for(unsigned i = 0; i < B_height * B_width; ++i) { input_b[i] = rand_float(); } #else for(unsigned i = 0; i < num_devices; ++i) { input_a[i].reset(context, rows_per_device[i] * A_width); output[i].reset(context, rows_per_device[i] * C_width); cl_int status; status = clEnqueueSVMMap(queue[i], CL_TRUE, CL_MAP_WRITE, (void *)input_a[i], rows_per_device[i] * A_width * sizeof(float), 0, NULL, NULL); checkError(status, "Failed to map input A"); for(unsigned j = 0; j < rows_per_device[i] * A_width; ++j) { input_a[i][j] = rand_float(); } status = clEnqueueSVMUnmap(queue[i], (void *)input_a[i], 0, NULL, NULL); checkError(status, "Failed to unmap input A"); } input_b.reset(context, B_height * B_width); cl_int status; for (unsigned i = 0; i < num_devices; ++i) { status = clEnqueueSVMMap(queue[i], CL_TRUE, CL_MAP_WRITE, (void *)input_b, B_height * B_width * sizeof(float), 0, NULL, NULL); checkError(status, "Failed to map input B"); } for(unsigned i = 0; i < B_height * B_width; ++i) { input_b[i] = rand_float(); } for (unsigned i = 0; i < num_devices; ++i) { status = clEnqueueSVMUnmap(queue[i], (void *)input_b, 0, NULL, NULL); checkError(status, "Failed to unmap input B"); } #endif /* USE_SVM_API == 0 */ }
bool initProblem() { string genome_path("K:\\nn\\opencl\\altera\\fnn\\simple_genome.xml"); ifstream infile(genome_path); if (!infile.is_open()) { printf("Failed to open genome file."); return false; } string line; getline(infile, line); nodesCount = atoi(line.c_str()); getline(infile, line); inputsCount = atoi(line.c_str()); getline(infile, line); outputsCount = atoi(line.c_str()); getline(infile, line); layersCount = atoi(line.c_str()); getline(infile, line); string buf; // Have a buffer string stringstream ssWeights(line); // Insert the string into a stream while (ssWeights >> buf) weights.push_back(strtod(buf.c_str(), 0)); connsCount = weights.size(); // source connections getline(infile, line); stringstream ssSrcConns(line); while (ssSrcConns >> buf) srcConns.push_back(atoi(buf.c_str())); // target connections getline(infile, line); stringstream ssTgtConns(line); while (ssTgtConns >> buf) tgtConns.push_back(atoi(buf.c_str())); // layers end connections getline(infile, line); stringstream ssEndConns(line); while (ssEndConns >> buf) endConns.push_back(atoi(buf.c_str())); // layers end nodes getline(infile, line); stringstream ssEndNodes(line); while (ssEndNodes >> buf) endNodes.push_back(atoi(buf.c_str())); // output ids getline(infile, line); stringstream ssOut(line); while (ssOut >> buf) outputIds.push_back(atoi(buf.c_str())); string validation_set("K:\\nn\\opencl\\altera\\fnn\\cross-validate.txt"); ifstream vs_infile(validation_set); if (!vs_infile.is_open()) { printf("Failed to open validation dataset."); return false; } float feature; while(getline(vs_infile, line)) { stringstream ssIrisDatasetLine(line); vector<float> row; while (ssIrisDatasetLine >> feature) { //printf((line + "\n").c_str()); row.push_back(feature); //printf("Add item %f from row %s \n", feature, line.c_str()); if(ssIrisDatasetLine.peek() == ';') { ssIrisDatasetLine.ignore(); } } validationSet.push_back(row); } if(validationSet.size() == 0) { printf("Failed to parse validation dataset."); return false; } //printf("First row size %llu", validationSet[0].size()); inputValuesAligned.reset(validationSet[0].size() - 3); for(int i = 0; i < 4; i++) { inputValuesAligned[i] = validationSet[0][i]; } initAligned(); return true; }