void Ableton::update() { while(receiver.hasWaitingMessages()) { ofxOscMessage m; receiver.getNextMessage(&m); displayOscMessage(m); if (m.getAddress() == "/live/device/param") { getParameterUpdate(m); } else if (m.getAddress() == "/live/devicelist") { getDeviceList(m); } else if (m.getAddress() == "/live/device/allparam") { getDeviceParameters(m); } else if (m.getAddress() == "/live/device/range") { getDeviceParametersRange(m); } else if (m.getAddress() == "/live/scenes") { getNumScenes(m); } else if (m.getAddress() == "/live/tracks") { getNumTracks(m); } else { displayOscMessage(m); } } }
int VideoCaptureV4L2::listDevices() { std::vector<LinuxCaptureDevice> devices = getDeviceList(); if(!devices.size()) { printf("WARNING: no video4linux devices found\n"); return 0; } struct v4l2_input inputs[V4L2_MAX_QUERYABLE_INPUTS]; int c = 0; for(std::vector<LinuxCaptureDevice>::iterator it = devices.begin(); it != devices.end(); ++it) { printf("[%d] %s, (idVendor: %s, idProduct: %s)\n", c++, (*it).path.c_str(), (*it).id_vendor.c_str(), (*it).id_product.c_str()); } return devices.size(); }
//------------------------------------------------------------ vector<ofSoundDevice> ofSoundStream::getMatchingDevices(const std::string& name, unsigned int inChannels, unsigned int outChannels) const { vector<ofSoundDevice> devs = getDeviceList(); vector<ofSoundDevice> hits; for(size_t i = 0; i < devs.size(); i++) { bool nameMatch = devs[i].name.find(name) != string::npos; bool inMatch = (inChannels == UINT_MAX) || (devs[i].inputChannels == inChannels); bool outMatch = (outChannels == UINT_MAX) || (devs[i].outputChannels == outChannels); if(nameMatch && inMatch && outMatch) { hits.push_back(devs[i]); } } return hits; }
QString CameraDevice::getDefaultDeviceName() { QString defaultdev = Settings::getInstance().getVideoDev(); if (!getDefaultInputFormat()) return defaultdev; QVector<QPair<QString, QString>> devlist = getDeviceList(); for (const QPair<QString,QString>& device : devlist) if (defaultdev == device.first) return defaultdev; if (devlist.isEmpty()) return defaultdev; return devlist[0].first; }
//-------------------------------------------------------------------------------- bool ofxKinectV2::open(unsigned int deviceId){ vector <KinectDeviceInfo> devices = getDeviceList(); if( devices.size() == 0 ){ ofLogError("ofxKinectV2::open") << "no devices connected!"; return false; } if( deviceId >= devices.size() ){ ofLogError("ofxKinectV2::open") << " deviceId " << deviceId << " is bigger or equal to the number of connected devices " << devices.size() << endl; return false; } string serial = devices[deviceId].serial; return open(serial); }
int main(int argc, char *argv[]) { float *h_psum; // vector to hold partial sum int in_nsteps = INSTEPS; // default number of steps (updated later to device prefereable) int niters = ITERS; // number of iterations int nsteps; float step_size; ::size_t nwork_groups; ::size_t max_size, work_group_size = 8; float pi_res; cl::Buffer d_partial_sums; try { cl_uint deviceIndex = 0; parseArguments(argc, argv, &deviceIndex); // Get list of devices std::vector<cl::Device> devices; unsigned numDevices = getDeviceList(devices); // Check device index in range if (deviceIndex >= numDevices) { std::cout << "Invalid device index (try '--list')\n"; return EXIT_FAILURE; } cl::Device device = devices[deviceIndex]; std::string name; getDeviceName(device, name); std::cout << "\nUsing OpenCL device: " << name << "\n"; std::vector<cl::Device> chosen_device; chosen_device.push_back(device); cl::Context context(chosen_device); cl::CommandQueue queue(context, device); // Create the program object cl::Program program(context, util::loadProgram("../pi_ocl.cl"), true); // Create the kernel object for quering information cl::Kernel ko_pi(program, "pi"); // Get the work group size work_group_size = ko_pi.getWorkGroupInfo<CL_KERNEL_WORK_GROUP_SIZE>(device); //printf("wgroup_size = %lu\n", work_group_size); cl::make_kernel<int, float, cl::LocalSpaceArg, cl::Buffer> pi(program, "pi"); // Now that we know the size of the work_groups, we can set the number of work // groups, the actual number of steps, and the step size nwork_groups = in_nsteps/(work_group_size*niters); if ( nwork_groups < 1) { nwork_groups = device.getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>(); work_group_size=in_nsteps / (nwork_groups*niters); } nsteps = work_group_size * niters * nwork_groups; step_size = 1.0f/static_cast<float>(nsteps); std::vector<float> h_psum(nwork_groups); printf( " %d work groups of size %d. %d Integration steps\n", (int)nwork_groups, (int)work_group_size, nsteps); d_partial_sums = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * nwork_groups); util::Timer timer; // Execute the kernel over the entire range of our 1d input data set // using the maximum number of work group items for this device pi( cl::EnqueueArgs( queue, cl::NDRange(nsteps / niters), cl::NDRange(work_group_size)), niters, step_size, cl::Local(sizeof(float) * work_group_size), d_partial_sums); cl::copy(queue, d_partial_sums, h_psum.begin(), h_psum.end()); // complete the sum and compute final integral value pi_res = 0.0f; for (unsigned int i = 0; i< nwork_groups; i++) { pi_res += h_psum[i]; } pi_res = pi_res * step_size; //rtime = wtime() - rtime; double rtime = static_cast<double>(timer.getTimeMilliseconds()) / 1000.; printf("\nThe calculation ran in %lf seconds\n", rtime); printf(" pi = %f for %d steps\n", pi_res, nsteps); } catch (cl::Error err) { std::cout << "Exception\n"; std::cerr << "ERROR: " << err.what() << "(" << err_code(err.err()) << ")" << std::endl; } }
/* * Open an FTDI device. The device is selected with regard to the given restrictions; * any of those may be NULL and index is counted in the subset selected by the * description and/or serial if given. * * Returns: true if successfully opened, false otherwise. */ bool FtdiDevice::open( const char* description, const char* serial, int index ) { hasFtdiError_ = false; if ( isOpen() ) { hasFtdiError_ = false; return false; } context_ = ftdi_new(); if ( context_ == 0 ) { hasFtdiError_ = true; return false; } bool success = false; vec_deviceInfo* devs = const_cast<vec_deviceInfo*>( getDeviceList( context_ ) ); vec_deviceInfo::iterator it; if ( devs != 0 ) { for ( it = devs->begin(); it != devs->end(); ++it ) { const deviceInfo& di = *it; if ( description != 0 || serial != 0 ) { if ( di.usbInfo == 0 ) break; if ( description != 0 && std::strncmp( di.usbInfo->description, description, std::strlen( description ) ) != 0 ) continue; if ( serial != 0 && std::strncmp( di.usbInfo->serial, serial, std::strlen( serial ) ) != 0 ) continue; } if ( index-- != 0 ) continue; //If control gets here, we have found a suitable device. if ( ftdi_usb_open_dev( context_, di.ftdiDevice ) < 0 ) { //do not free context since that would also free the error message hasFtdiError_ = true; } usbInfo_ = new usbInformation( *( di.usbInfo ) ); success = true; } } else { hasFtdiError_ = ( devs == 0 ); } if ( ! success && ! hasFtdiError_ ) { delete context_; context_ = 0; } if ( success ) { purgeBuffers(); reset(); } return success; }
static BOOL handleCommand(char line[]) { BOOL ret = TRUE; if (strcmp(line, "q") == 0) ret = FALSE; else if (strcmp(line, "o") == 0 && gDeviceHandle == INVALID_HANDLE_VALUE) openDriver(); else if (strcmp(line, "c") == 0 && gDeviceHandle != INVALID_HANDLE_VALUE) closeDriver(); else if (strcmp(line, "g") == 0 && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize == 0) getDeviceList(); else if (strcmp(line, "p") == 0 && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) printDeviceList(); else if (strcmp(line, "r") == 0 && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) releaseDeviceList(); else if (line[0] == 'm' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) sendControlRequest(line + 1); else if (line[0] == 'a' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) sendAapRequest(line + 1); else if (line[0] == 'c' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) requestConfigurationDescriptor(line + 1); else if (line[0] == 'o' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) getActiveConfigValue(line + 1); else if (line[0] == 's' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) setActiveConfigValue(line + 1); else if (line[0] == 'i' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) performInterfaceOperation(line + 1); else if (line[0] == 'r' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) resetDevice(line + 1); else if (line[0] == 'k' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) performKernelAttachOperation(line + 1); else if (line[0] == 'b' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) startAAPBulkTransfer(line + 1); else if (line[0] == 'h' && gDeviceHandle != INVALID_HANDLE_VALUE && gDeviceListSize > 0) performHaltOperation(line + 1); else printf("Unknown command '%s'\n", line); return ret; }
//------------------------------------------------------------ vector<ofSoundDevice> ofSoundStream::listDevices() const{ vector<ofSoundDevice> deviceList = getDeviceList(); ofLogNotice("ofSoundStream::listDevices") << std::endl << deviceList; return deviceList; }
int main(int argc, char *argv[]) { float *h_A; // A matrix float *h_B; // B matrix float *h_C; // C = A*B matrix int N; // A[N][N], B[N][N], C[N][N] int size; // number of elements in each matrix cl_mem d_a, d_b, d_c; // Matrices in device memory double start_time; // Starting time double run_time; // timing data char * kernelsource; // kernel source string cl_int err; // error code returned from OpenCL calls cl_device_id device; // compute device id cl_context context; // compute context cl_command_queue commands; // compute command queue cl_program program; // compute program cl_kernel kernel; // compute kernel N = ORDER; size = N * N; h_A = (float *)malloc(size * sizeof(float)); h_B = (float *)malloc(size * sizeof(float)); h_C = (float *)malloc(size * sizeof(float)); //-------------------------------------------------------------------------------- // Create a context, queue and device. //-------------------------------------------------------------------------------- cl_uint deviceIndex = 0; parseArguments(argc, argv, &deviceIndex); // Get list of devices cl_device_id devices[MAX_DEVICES]; unsigned numDevices = getDeviceList(devices); // Check device index in range if (deviceIndex >= numDevices) { printf("Invalid device index (try '--list')\n"); return EXIT_FAILURE; } device = devices[deviceIndex]; char name[MAX_INFO_STRING]; getDeviceName(device, name); printf("\nUsing OpenCL device: %s\n", name); // Create a compute context context = clCreateContext(0, 1, &device, NULL, NULL, &err); checkError(err, "Creating context"); // Create a command queue commands = clCreateCommandQueue(context, device, 0, &err); checkError(err, "Creating command queue"); //-------------------------------------------------------------------------------- // Run sequential version on the host //-------------------------------------------------------------------------------- initmat(N, h_A, h_B, h_C); printf("\n===== Sequential, matrix mult (dot prod), order %d on host CPU ======\n",ORDER); for(int i = 0; i < COUNT; i++) { zero_mat(N, h_C); start_time = wtime(); seq_mat_mul_sdot(N, h_A, h_B, h_C); run_time = wtime() - start_time; results(N, h_C, run_time); } //-------------------------------------------------------------------------------- // Setup the buffers, initialize matrices, and write them into global memory //-------------------------------------------------------------------------------- // Reset A, B and C matrices (just to play it safe) initmat(N, h_A, h_B, h_C); d_a = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(float) * size, h_A, &err); checkError(err, "Creating buffer d_a"); d_b = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof(float) * size, h_B, &err); checkError(err, "Creating buffer d_b"); d_c = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * size, NULL, &err); checkError(err, "Creating buffer d_c"); //-------------------------------------------------------------------------------- // OpenCL matrix multiplication ... Naive //-------------------------------------------------------------------------------- kernelsource = getKernelSource("../C_elem.cl"); // Create the comput program from the source buffer program = clCreateProgramWithSource(context, 1, (const char **) & kernelsource, NULL, &err); checkError(err, "Creating program with C_elem.cl"); free(kernelsource); // Build the program err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (err != CL_SUCCESS) { size_t len; char buffer[2048]; printf("Error: Failed to build program executable!\n%s\n", err_code(err)); clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); printf("%s\n", buffer); return EXIT_FAILURE; } // Create the compute kernel from the program kernel = clCreateKernel(program, "mmul", &err); checkError(err, "Creating kernel from C_elem.cl"); printf("\n===== OpenCL, matrix mult, C(i,j) per work item, order %d ======\n",N); // Do the multiplication COUNT times for (int i = 0; i < COUNT; i++) { zero_mat(N, h_C); err = clSetKernelArg(kernel, 0, sizeof(int), &N); err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_a); err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &d_b); err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &d_c); checkError(err, "Setting kernel args"); start_time = wtime(); // Execute the kernel over the entire range of C matrix elements ... computing // a dot product for each element of the product matrix. The local work // group size is set to NULL ... so I'm telling the OpenCL runtime to // figure out a local work group size for me. const size_t global[2] = {N, N}; err = clEnqueueNDRangeKernel( commands, kernel, 2, NULL, global, NULL, 0, NULL, NULL); checkError(err, "Enqueueing kernel"); err = clFinish(commands); checkError(err, "Waiting for kernel to finish"); run_time = wtime() - start_time; err = clEnqueueReadBuffer( commands, d_c, CL_TRUE, 0, sizeof(float) * size, h_C, 0, NULL, NULL); checkError(err, "Copying back d_c"); results(N, h_C, run_time); } // end for loop //-------------------------------------------------------------------------------- // OpenCL matrix multiplication ... C row per work item //-------------------------------------------------------------------------------- kernelsource = getKernelSource("../C_row.cl"); // Create the comput program from the source buffer program = clCreateProgramWithSource(context, 1, (const char **) & kernelsource, NULL, &err); checkError(err, "Creating program with C_row.cl"); free(kernelsource); // Build the program err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (err != CL_SUCCESS) { size_t len; char buffer[2048]; printf("Error: Failed to build program executable!\n%s\n", err_code(err)); clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); printf("%s\n", buffer); return EXIT_FAILURE; } // Create the compute kernel from the program kernel = clCreateKernel(program, "mmul", &err); checkError(err, "Creating kernel from C_row.cl"); printf("\n===== OpenCL, matrix mult, C row per work item, order %d ======\n",N); // Do the multiplication COUNT times for (int i = 0; i < COUNT; i++) { zero_mat(N, h_C); err = clSetKernelArg(kernel, 0, sizeof(int), &N); err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_a); err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &d_b); err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &d_c); checkError(err, "Setting kernel args"); start_time = wtime(); // Execute the kernel over the rows of the C matrix ... computing // a dot product for each element of the product matrix. const size_t global = N; err = clEnqueueNDRangeKernel( commands, kernel, 1, NULL, &global, NULL, 0, NULL, NULL); checkError(err, "Enqueueing kernel"); err = clFinish(commands); checkError(err, "Waiting for kernel to finish"); run_time = wtime() - start_time; err = clEnqueueReadBuffer( commands, d_c, CL_TRUE, 0, sizeof(float) * size, h_C, 0, NULL, NULL); checkError(err, "Reading back d_c"); results(N, h_C, run_time); } // end for loop //-------------------------------------------------------------------------------- // OpenCL matrix multiplication ... C row per work item, A row in pivate memory //-------------------------------------------------------------------------------- kernelsource = getKernelSource("../C_row_priv.cl"); // Create the comput program from the source buffer program = clCreateProgramWithSource(context, 1, (const char **) & kernelsource, NULL, &err); checkError(err, "Creating program from C_row_priv.cl"); free(kernelsource); // Build the program err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (err != CL_SUCCESS) { size_t len; char buffer[2048]; printf("Error: Failed to build program executable!\n%s\n", err_code(err)); clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); printf("%s\n", buffer); return EXIT_FAILURE; } // Create the compute kernel from the program kernel = clCreateKernel(program, "mmul", &err); checkError(err, "Creating kernel from C_row_priv.cl"); printf("\n===== OpenCL, matrix mult, C row, A row in priv mem, order %d ======\n",N); // Do the multiplication COUNT times for (int i = 0; i < COUNT; i++) { zero_mat(N, h_C); err = clSetKernelArg(kernel, 0, sizeof(int), &N); err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_a); err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &d_b); err |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &d_c); checkError(err, "Setting kernel args"); start_time = wtime(); // Execute the kernel over the rows of the C matrix ... computing // a dot product for each element of the product matrix. const size_t global = N; const size_t local = ORDER / 16; err = clEnqueueNDRangeKernel( commands, kernel, 1, NULL, &global, &local, 0, NULL, NULL); checkError(err, "Enqueueing kernel"); err = clFinish(commands); checkError(err, "Waiting for kernel to finish"); run_time = wtime() - start_time; err = clEnqueueReadBuffer( commands, d_c, CL_TRUE, 0, sizeof(float) * size, h_C, 0, NULL, NULL); checkError(err, "Reading back d_c"); results(N, h_C, run_time); } // end for loop //-------------------------------------------------------------------------------- // Clean up! //-------------------------------------------------------------------------------- free(h_A); free(h_B); free(h_C); clReleaseMemObject(d_a); clReleaseMemObject(d_b); clReleaseMemObject(d_c); clReleaseProgram(program); clReleaseKernel(kernel); clReleaseCommandQueue(commands); clReleaseContext(context); return EXIT_SUCCESS; }
//-------------------------------------------------------------------------------- unsigned int ofxKinectV2::getNumDevices(){ return getDeviceList().size(); }
int main(int argc, char *argv[]) { float *h_psum; // vector to hold partial sum int in_nsteps = INSTEPS; // default number of steps (updated later to device preferable) int niters = ITERS; // number of iterations int nsteps; float step_size; size_t nwork_groups; size_t max_size, work_group_size = 8; float pi_res; cl_mem d_partial_sums; char *kernelsource = getKernelSource("../pi_ocl.cl"); // Kernel source cl_int err; cl_device_id device; // compute device id cl_context context; // compute context cl_command_queue commands; // compute command queue cl_program program; // compute program cl_kernel kernel_pi; // compute kernel // Set up OpenCL context, queue, kernel, etc. cl_uint deviceIndex = 0; parseArguments(argc, argv, &deviceIndex); // Get list of devices cl_device_id devices[MAX_DEVICES]; unsigned numDevices = getDeviceList(devices); // Check device index in range if (deviceIndex >= numDevices) { printf("Invalid device index (try '--list')\n"); return EXIT_FAILURE; } device = devices[deviceIndex]; char name[MAX_INFO_STRING]; getDeviceName(device, name); printf("\nUsing OpenCL device: %s\n", name); // Create a compute context context = clCreateContext(0, 1, &device, NULL, NULL, &err); checkError(err, "Creating context"); // Create a command queue commands = clCreateCommandQueue(context, device, 0, &err); checkError(err, "Creating command queue"); // Create the compute program from the source buffer program = clCreateProgramWithSource(context, 1, (const char **) & kernelsource, NULL, &err); checkError(err, "Creating program"); // Build the program err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (err != CL_SUCCESS) { size_t len; char buffer[2048]; printf("Error: Failed to build program executable!\n%s\n", err_code(err)); clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len); printf("%s\n", buffer); return EXIT_FAILURE; } // Create the compute kernel from the program kernel_pi = clCreateKernel(program, "pi", &err); checkError(err, "Creating kernel"); // Find kernel work-group size err = clGetKernelWorkGroupInfo (kernel_pi, device, CL_KERNEL_WORK_GROUP_SIZE, sizeof(size_t), &work_group_size, NULL); checkError(err, "Getting kernel work group info"); // Now that we know the size of the work-groups, we can set the number of // work-groups, the actual number of steps, and the step size nwork_groups = in_nsteps/(work_group_size*niters); if (nwork_groups < 1) { err = clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(size_t), &nwork_groups, NULL); checkError(err, "Getting device compute unit info"); work_group_size = in_nsteps / (nwork_groups * niters); } nsteps = work_group_size * niters * nwork_groups; step_size = 1.0f/(float)nsteps; printf("nsteps:%d\n", nsteps); printf("niters:%d\n", niters); printf("work_group_size:%zd\n", work_group_size); printf("n work groups:%ld\n", nwork_groups); printf("step_size:%f\n", step_size); h_psum = calloc(sizeof(float), nwork_groups); if (!h_psum) { printf("Error: could not allocate host memory for h_psum\n"); return EXIT_FAILURE; } printf(" %ld work-groups of size %ld. %d Integration steps\n", nwork_groups, work_group_size, nsteps); d_partial_sums = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * nwork_groups, NULL, &err); checkError(err, "Creating buffer d_partial_sums"); // Set kernel arguments err = clSetKernelArg(kernel_pi, 0, sizeof(int), &niters); err |= clSetKernelArg(kernel_pi, 1, sizeof(float), &step_size); err |= clSetKernelArg(kernel_pi, 2, sizeof(float) * work_group_size, NULL); err |= clSetKernelArg(kernel_pi, 3, sizeof(cl_mem), &d_partial_sums); checkError(err, "Settin kernel args"); // Execute the kernel over the entire range of our 1D input data set // using the maximum number of work items for this device size_t global = nsteps / niters; size_t local = work_group_size; double rtime = wtime(); err = clEnqueueNDRangeKernel( commands, kernel_pi, 1, NULL, &global, &local, 0, NULL, NULL); checkError(err, "Enqueueing kernel"); err = clEnqueueReadBuffer( commands, d_partial_sums, CL_TRUE, 0, sizeof(float) * nwork_groups, h_psum, 0, NULL, NULL); checkError(err, "Reading back d_partial_sums"); // complete the sum and compute the final integral value on the host pi_res = 0.0f; for (unsigned int i = 0; i < nwork_groups; i++) { pi_res += h_psum[i]; } pi_res *= step_size; rtime = wtime() - rtime; printf("\nThe calculation ran in %lf seconds\n", rtime); printf(" pi = %f for %d steps\n", pi_res, nsteps); // clean up clReleaseMemObject(d_partial_sums); clReleaseProgram(program); clReleaseKernel(kernel_pi); clReleaseCommandQueue(commands); clReleaseContext(context); free(kernelsource); free(h_psum); }
QueryData genDrivers(QueryContext& context) { QueryData results; auto devInfoset = setupDevInfoSet(); if (devInfoset == nullptr) { win32LogWARNING("Error getting device handle"); return results; } std::vector<SP_DEVINFO_DATA> devices; auto ret = getDeviceList(devInfoset, devices); if (!ret.ok()) { win32LogWARNING(ret.getMessage(), ret.getCode()); return results; } for (auto& device : devices) { char devId[MAX_DEVICE_ID_LEN] = {0}; if (CM_Get_Device_ID(device.DevInst, devId, MAX_DEVICE_ID_LEN, 0) != CR_SUCCESS) { win32LogWARNING("Failed to get device ID"); return QueryData(); } SP_DRVINFO_DATA drvInfo = {0}; SP_DRVINFO_DETAIL_DATA drvInfoDetail = {0}; ret = getDeviceDriverInfo(devInfoset, device, drvInfo, drvInfoDetail); Row r; r["device_id"] = devId; r["inf"] = drvInfoDetail.InfFileName; r["provider"] = drvInfo.ProviderName; r["manufacturer"] = drvInfo.MfgName; r["date"] = std::to_string(osquery::filetimeToUnixtime(drvInfo.DriverDate)); r["description"] = drvInfo.Description; ULARGE_INTEGER version; version.QuadPart = drvInfo.DriverVersion; r["version"] = std::to_string(HIWORD(version.HighPart)) + "." + std::to_string(HIWORD(version.LowPart)) + "." + std::to_string(LOWORD(version.HighPart)) + "." + std::to_string(LOWORD(version.LowPart)); for (const auto& elem : kAdditionalDeviceProps) { std::string val; ret = getDeviceProperty(devInfoset, device, elem.second, val); r[elem.first] = std::move(val); } if (r.count("driver_key") > 0) { if (!r.at("driver_key").empty()) { r["driver_key"].insert(0, kDriverKeyPath); } } if (r.count("service") > 0) { if (!r.at("service").empty()) { r["service_key"] = kServiceKeyPath + r["service"]; r["image"] = getDriverImagePath(r["service_key"]); } } results.push_back(r); } return results; }
int main(int argc, char *argv[]) { int N; // A[N][N], B[N][N], C[N][N] int size; // Number of elements in each matrix double start_time; // Starting time double run_time; // Timing util::Timer timer; // Timing N = ORDER; size = N * N; std::vector<float> h_A(size); // Host memory for Matrix A std::vector<float> h_B(size); // Host memory for Matrix B std::vector<float> h_C(size); // Host memory for Matrix C cl::Buffer d_a, d_b, d_c; // Matrices in device memory //-------------------------------------------------------------------------------- // Create a context and queue //-------------------------------------------------------------------------------- try { cl_uint deviceIndex = 0; parseArguments(argc, argv, &deviceIndex); // Get list of devices std::vector<cl::Device> devices; unsigned numDevices = getDeviceList(devices); // Check device index in range if (deviceIndex >= numDevices) { std::cout << "Invalid device index (try '--list')\n"; return EXIT_FAILURE; } cl::Device device = devices[deviceIndex]; std::string name; getDeviceName(device, name); std::cout << "\nUsing OpenCL device: " << name << "\n"; std::vector<cl::Device> chosen_device; chosen_device.push_back(device); cl::Context context(chosen_device); cl::CommandQueue queue(context, device); //-------------------------------------------------------------------------------- // Run sequential matmul //-------------------------------------------------------------------------------- initmat(N, h_A, h_B, h_C); timer.reset(); printf("\n===== Sequential, matrix mult (dot prod), order %d on host CPU ======\n",N); for(int i = 0; i < COUNT; i++) { zero_mat(N, h_C); start_time = static_cast<double>(timer.getTimeMilliseconds()) / 1000.0; seq_mat_mul_sdot(N, h_A, h_B, h_C); run_time = static_cast<double>(timer.getTimeMilliseconds()) / 1000.0 - start_time; results(N, h_C, run_time); } //-------------------------------------------------------------------------------- // Setup the buffers, initialize matrices, and write them into global memory //-------------------------------------------------------------------------------- // Reset A, B and C matrices (just to play it safe) initmat(N, h_A, h_B, h_C); d_a = cl::Buffer(context, h_A.begin(), h_A.end(), true); d_b = cl::Buffer(context, h_B.begin(), h_B.end(), true); d_c = cl::Buffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * size); //-------------------------------------------------------------------------------- // OpenCL matrix multiplication ... Naive //-------------------------------------------------------------------------------- // Create the compute program from the source buffer cl::Program program(context, kernelsource, true); // Create the compute kernel from the program cl::make_kernel<int, cl::Buffer, cl::Buffer, cl::Buffer> naive_mmul(program, "mmul"); printf("\n===== OpenCL, matrix mult, C(i,j) per work item, order %d ======\n",N); // Do the multiplication COUNT times for (int i = 0; i < COUNT; i++) { zero_mat(N, h_C); start_time = static_cast<double>(timer.getTimeMilliseconds()) / 1000.0; // Execute the kernel over the entire range of C matrix elements ... computing // a dot product for each element of the product matrix. The local work // group size is set to NULL ... so I'm telling the OpenCL runtime to // figure out a local work group size for me. cl::NDRange global(N, N); naive_mmul(cl::EnqueueArgs(queue, global), N, d_a, d_b, d_c); queue.finish(); run_time = static_cast<double>(timer.getTimeMilliseconds()) / 1000.0 - start_time; cl::copy(queue, d_c, h_C.begin(), h_C.end()); results(N, h_C, run_time); } // end for loop } catch (cl::Error err) { std::cout << "Exception\n"; std::cerr << "ERROR: " << err.what() << "(" << err_code(err.err()) << ")" << std::endl; } return EXIT_SUCCESS; }
int VideoCaptureV4L2::openDevice(int device, int w, int h, VideoCaptureFormat capFormat) { std::vector<LinuxCaptureDevice> devices = getDeviceList(); if(device >= devices.size()) { return 0; } LinuxCaptureDevice cap = devices[device]; fd = open(cap.path.c_str(), O_RDWR | O_NONBLOCK, 0); if(fd == -1) { printf("ERROR: cannot open: %s\n", cap.path.c_str()); fd = 0; return 0; } // query capabilities struct v4l2_capability caps; memset(&caps, 0, sizeof(caps)); if (ioctl(fd, VIDIOC_QUERYCAP, &caps) == -1) { printf("ERROR: cannot detect capabilities of camera.\n"); return 0; } // test if we can use this device as capture device if(!(caps.capabilities & V4L2_CAP_VIDEO_CAPTURE)) { printf("ERROR: cannot use device for video capture.\n"); return 0; } // check the io type io_method = LINCAP_IO_METHOD_READ; bool can_io_readwrite = (caps.capabilities & V4L2_CAP_READWRITE) == V4L2_CAP_READWRITE; bool can_io_stream = (caps.capabilities & V4L2_CAP_STREAMING) == V4L2_CAP_STREAMING; if(!can_io_readwrite && !can_io_stream) { printf("ERROR: cannot use read() or memory streaming with this device.\n"); return 0; } if(can_io_stream) { io_method = LINCAP_IO_METHOD_MMAP; } if(!isFormatSupported(capFormat, w, h, 1)) { printf("ERROR: cannot set pixelformat and size: %dx%d\n", w,h); return 0; } width = w; height = h; // initialize IO if(io_method == LINCAP_IO_METHOD_MMAP) { initMMAP(); } else if(io_method == LINCAP_IO_METHOD_READ) { printf("ERROR: this device can use read()/write() io, but we did not yet program this.\n"); ::exit(0); } else if(io_method == LINCAP_IO_METHOD_USERPTR) { printf("ERROR: this device can use i/o with userptr, but we need to program it.\n"); ::exit(EXIT_FAILURE); } is_opened = true; return 1; }