void try_loading(HANDLE hfile, global_routing_params*& params, LARGE_INTEGER size) { if(params != NULL) delete [] (unsigned char*)params; params = NULL; unsigned char* view_of_file = (unsigned char*)MapViewOfFile(hfile, FILE_MAP_READ, 0, 0, 0); unsigned char* buffer = NULL; __try { buffer = new unsigned char[size.QuadPart]; memcpy(buffer, view_of_file, size.QuadPart); UnmapViewOfFile(view_of_file); view_of_file = NULL; const size_t routing_params_size = global_size(params = rebase(buffer)); if(size.QuadPart != routing_params_size || !routing_params_size) // validates parameters throw; // will be catched at the parent except clause } __except(EXCEPTION_EXECUTE_HANDLER) { if(view_of_file) UnmapViewOfFile(view_of_file); if(buffer) delete [] buffer; params = NULL; throw std::wstring(L"The " LOCAL_PARAMS_FILE L" file is corrupted. Please delete the " \ L"file and open Audio Router again.\n"); } }
void nest::Subnet::get_status( DictionaryDatum& dict ) const { ( *dict )[ "number_of_children" ] = global_size(); ( *dict )[ "label" ] = label_; ( *dict )[ "customdict" ] = customdict_; ( *dict )[ names::element_type ] = LiteralDatum( names::structure ); }
void bootstrapper::update_save() { global_routing_params* routing_params = this->routing_params; global_routing_params default_params; create_default_params(default_params); if(!routing_params) routing_params = &default_params; // serialize params const size_t routing_params_size = global_size(routing_params); assert(routing_params_size > 0); if(routing_params_size == 0) throw std::wstring(L"The internal state of saved routings is corrupted.\n"); // update the file and create a view this->implicit_params.Close(); this->local_file.Close(); // TODO: do not loop infinitely do { this->local_file = CHandle(NULL); this->local_file.Attach(CreateFile(LOCAL_PARAMS_FILE, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL)); } while(this->local_file == INVALID_HANDLE_VALUE && GetLastError() == ERROR_USER_MAPPED_FILE); if(this->local_file == INVALID_HANDLE_VALUE) { throw std::wstring(L"Could not open file " LOCAL_PARAMS_FILE L" while saving routing. " \ L"Audio Router is left in an invalid state and must be restarted.\n"); } this->implicit_params.Attach(CreateFileMapping(this->local_file, NULL, PAGE_READWRITE, 0, (DWORD)routing_params_size, NULL)); if(!this->implicit_params) { this->local_file.Close(); throw std::wstring(L"Unexpected error occured. Old routed savings data is lost. " \ L"Audio Router is left in an invalid state and must be restarted.\n"); } unsigned char* view_of_file = (unsigned char*)MapViewOfFile(this->implicit_params, FILE_MAP_ALL_ACCESS, 0, 0, 0); if(!view_of_file) { this->implicit_params.Close(); this->local_file.Close(); throw std::wstring(L"Unexpected error occured while mapping. Old routed savings data " \ L"is lost. Audio Router is left in an invalid state and must be restarted.\n"); } serialize(routing_params, view_of_file); FlushViewOfFile(view_of_file, 0); UnmapViewOfFile(view_of_file); this->implicit_params.Close(); this->local_file.Close(); }
std::vector<Node*>::const_iterator AbstractLayer::local_end(int_t depth) const { if (depth >= depth_) throw BadProperty("Selected depth out of range"); index min_nodes_per_layer = local_size()/depth_; index last_gid_at_depth = gids_[(depth+1)*(global_size()/depth_)-1]; std::vector<Node*>::const_iterator iter = local_begin(); for(iter += (depth+1)*min_nodes_per_layer; iter != local_end(); ++iter) { if ((*iter)->get_gid() > last_gid_at_depth) break; } return iter; }
int main(int argc, char** argv) { try { std::vector<cl::Platform> platforms; cl::Platform::get(&platforms); std::vector<cl::Device> platformDevices; platforms[0].getDevices(CL_DEVICE_TYPE_GPU, &platformDevices); cl::Context context(platformDevices); auto contextDevices = context.getInfo<CL_CONTEXT_DEVICES>(); for (auto dev : contextDevices) { std::cout << "Using " << dev.getInfo<CL_DEVICE_NAME>() << std::endl; } std::ifstream programFile("mandelbrot.cl"); std::string programString(std::istreambuf_iterator<char>(programFile), (std::istreambuf_iterator<char>())); cl::Program::Sources source(1, std::make_pair(programString.c_str(), programString.length()+1)); cl::Program program(context, source); try { program.build(contextDevices); } catch (cl::Error e) { // FIXME may not be the device that failed std::cout << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(contextDevices[0]) << std::endl; } cl::Kernel mandelbrot(program, "mandelbrot"); // command queues std::vector<cl::CommandQueue> queues; for (auto device : contextDevices) { cl::CommandQueue queue(context, device, CL_QUEUE_PROFILING_ENABLE); queues.push_back(queue); } unsigned char* iteration_counts = new unsigned char[3500*2500]; auto start = std::chrono::high_resolution_clock::now(); // partition the "y" dimension int i = 0; int workItemsPerQueue = 2500/queues.size(); // FIXME requires work size to be evenly divisible by number of queues std::vector<cl::Buffer> outputs; for (auto queue : queues) { cl::NDRange offset(0, 0); //i*workItemsPerQueue); cl::NDRange global_size(3500, workItemsPerQueue); // storage for results per device cl_int err = CL_SUCCESS; cl::Buffer output(context, CL_MEM_WRITE_ONLY, (size_t)3500*workItemsPerQueue, (void*)NULL, &err); mandelbrot.setArg(0, output); mandelbrot.setArg(1, i*workItemsPerQueue); outputs.push_back(output); queue.enqueueNDRangeKernel(mandelbrot, offset, global_size); queue.enqueueBarrierWithWaitList(); std::cout << "enqueued range " << i*workItemsPerQueue << " of length " << workItemsPerQueue << std::endl; i++; } // read results unsigned char* results = new unsigned char[3500*2500]; std::vector<cl::Event> readWaitList; i = 0; for (auto queue : queues) { size_t offset = i*3500*workItemsPerQueue; cl::Event readDoneEvent; queue.enqueueReadBuffer(outputs[i], CL_FALSE, 0, 3500*workItemsPerQueue, &(results[offset]), NULL, &readDoneEvent); // NOTE: can't push onto vector until the event is valid, since it will be copied readWaitList.push_back(readDoneEvent); i++; } cl::Event::waitForEvents(readWaitList); auto end = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> elapsed_seconds = end - start; std::cout << "computation took " << elapsed_seconds.count() << "s" << std::endl; stbi_write_png("mandelbrot_cl.png", 3500, 2500, 1, results, 3500); } catch (cl::Error e) { std::cout << e.what() << ": Error code " << e.err() << std::endl; } return 0; }