oneMKL Summary Statistics Usage Model

A typical algorithm for random number generators is as follows:

  1. Create and initialize the object for the dataset.

  2. Call the summary statistics routine to calculate the appropriate estimate.

The following example demonstrates how to calculate mean values for a 3-dimentional dataset filled with random numbers. For dataset creation, the make_dataset helper function is used.

Example of Summary Statistics Usage

Buffer API

#include <iostream>
#include <vector>


#include “CL/sycl.hpp”
#include “oneapi/mkl/stats.hpp”


int main() {
    sycl::queue queue;


    const size_t n_observations = 1000;
    const size_t n_dims = 3;
    std::vector<float> x(n_observations * n_dims);
    // fill x storage with random numbers
    for(int i = 0; i < n_dims, i++) {
        for(int j = 0; j < n_observations; j++) {
            x[j + i * n_observations] = float(std::rand()) / float(RAND_MAX)
        }
    }
    //create buffer for dataset
    sycl::buffer<float, 1> x_buf(x.data(), x.size());
    // create buffer for mean values
    sycl::buffer<float, 1> mean_buf(n_dims);
    // create mkl::stats::dataset
    auto dataset = oneapi::mkl::stats::make_dataset<mkl::stats::layout::row_major>(n_dims, n_observations, x_buf);


    oneapi::mkl::stats::mean(queue, dataset, mean_buf);


    // create host accessor for mean_buf to print results
    auto acc = mean_buf.template get_access<sycl::access::mode::read>();


    for(int i = 0; i < n_dims; i++) {
        std::cout << “Mean value for dimension ” << i << “: ”<< acc[i]<<
    std::endl;
    }
    return 0;
}

USM API

#include <iostream>
#include <vector>


#include “CL/sycl.hpp”
#include “oneapi/mkl/stats.hpp”


int main() {
    sycl::queue queue;


    const size_t n_observations = 1000;
    const size_t n_dims = 3;


    sycl::usm_allocator<float, sycl::usm::alloc::shared> allocator(queue);


    std::vector<float, decltype(allocator)> x(n_observations * n_dims, allocator);
    // fill x storage with random numbers
    for(int i = 0; i < n_dims, i++) {
        for(int j = 0; j < n_observations; j++) {
            x[j + i * n_observations] = float(std::rand()) / float(RAND_MAX)
        }
    }
    std::vector<float, decltype(allocator)> mean_buf(n_dims, allocator);
    // create mkl::stats::dataset
    auto dataset = oneapi::mkl::stats::make_dataset<mkl::stats::layout::row_major>(n_dims,  n_observations, x);


    sycl::event event = oneapi::mkl::stats::mean(queue, dataset, mean);
    event.wait();
   for(int i = 0; i < n_dims; i++) {
         std::cout << “Mean value for dimension ” << i << “: ”<< mean[i]<<
      std::endl;
   }
   return 0;
}

You can also use USM with raw pointers by using the sycl::malloc_shared/malloc_device functions. Additionally, examples that demonstrate usage of summary statistics functionality are available in: ${MKL}/examples/sycl/stats.