Skip to content

Commit b1a323e

Browse files
authored
[oneMKL][Stats][Spec] Simplify and clean up Summary Statistics domain (#494)
1 parent e494df3 commit b1a323e

File tree

2 files changed

+19
-85
lines changed

2 files changed

+19
-85
lines changed

source/elements/oneMKL/source/domains/stats/onemkl_stats_dataset.rst

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ structure dataset (Buffer version)
3030
3131
explicit dataset(std::int64_t n_dims_, std::int64_t n_observations_,
3232
sycl::buffer<Type, 1> observations_, sycl::buffer<Type, 1> weights_ = {0},
33-
sycl::buffer<std::int64_t, 1> indices_ = {0}) :
34-
n_dims(n_dims_), n_observations(n_observations_),
35-
observations(observations_),
36-
weights(weights_), indices(indices_) {};
33+
sycl::buffer<std::int64_t, 1> indices_ = {0});
3734
3835
std::int64_t n_dims;
3936
std::int64_t n_observations;
@@ -91,7 +88,7 @@ structure dataset (Buffer version)
9188
explicit dataset::dataset(std::int64_t n_dims_, std::int64_t n_observations_,
9289
sycl::buffer<Type, 1> observations_,
9390
sycl::buffer<Type, 1> weights_ = {0},
94-
sycl::buffer<std::int64_t, 1> indices_ = {0})
91+
sycl::buffer<std::int64_t, 1> indices_ = {0});
9592
9693
.. container:: section
9794

@@ -102,8 +99,8 @@ structure dataset (Buffer version)
10299
* `n_dims_` is the number of dimensions
103100
* `n_observations_` is the number of observations
104101
* `observations_` is the matrix of observations
105-
* `weights_` is an optional parameter, represents array of weights for observations (of size `n_observations`). If the parameter is not specified, each observation is assigned a weight equal 1.
106-
* `indices_` is an optional parameter, represents array of dimensions that are processed (of size `n_dims`). If the parameter is not specified, all dimensions are processed.
102+
* `weights_` is an optional parameter, represents an array of weights for observations (of size `n_observations`). If the parameter is not specified, each observation is assigned a weight equal 1.
103+
* `indices_` is an optional parameter, represents an array of dimensions that are processed (of size `n_dims`). If the parameter is not specified, all dimensions are processed.
107104

108105
.. container:: section
109106

@@ -125,10 +122,7 @@ structure dataset (USM version)
125122
template<layout ObservationsLayout, typename Type>
126123
struct dataset<Type*, ObservationsLayout> {
127124
explicit dataset(std::int64_t n_dims_, std::int64_t n_observations_, Type* observations_,
128-
Type* weights_ = nullptr, std::int64_t* indices_ = nullptr) :
129-
n_dims(n_dims_), n_observations(n_observations_),
130-
observations(observations_),
131-
weights(weights_), indices(indices_) {};
125+
Type* weights_ = nullptr, std::int64_t* indices_ = nullptr);
132126
133127
std::int64_t n_dims;
134128
std::int64_t n_observations;
@@ -186,7 +180,7 @@ structure dataset (USM version)
186180
explicit dataset::dataset(std::int64_t n_dims_, std::int64_t n_observations_,
187181
Type* observations_,
188182
Type* weights_ = nullptr,
189-
std::int64_t* indices_ = nullptr)
183+
std::int64_t* indices_ = nullptr);
190184
191185
.. container:: section
192186

@@ -197,8 +191,8 @@ structure dataset (USM version)
197191
* `n_dims_` is the number of dimensions
198192
* `n_observations_` is the number of observations
199193
* `observations_` is the matrix of observations
200-
* `weights_` is an optional parameter, represents array of weights for observations (of size `n_observations`). If the parameter is not specified, each observation is assigned a weight equal 1.
201-
* `indices_` is an optional parameter, represents array of dimensions that are processed (of size `n_dims`). If the parameter is not specified, all dimensions are processed.
194+
* `weights_` is an optional parameter, represents an array of weights for observations (of size `n_observations`). If the parameter is not specified, each observation is assigned a weight equal 1.
195+
* `indices_` is an optional parameter, represents an array of dimensions that are processed (of size `n_dims`). If the parameter is not specified, all dimensions are processed.
202196

203197
.. container:: section
204198

source/elements/oneMKL/source/domains/stats/onemkl_stats_usage_model.rst

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -20,92 +20,32 @@ A typical algorithm for summary statistics is as follows:
2020
The following example demonstrates how to calculate mean values for a 3-dimensional dataset filled with random numbers. For dataset creation, the :ref:`onemkl_stats_make_dataset` helper function is used.
2121

2222

23-
Buffer-based example
24-
--------------------
25-
26-
.. code-block:: cpp
27-
28-
#include <iostream>
29-
#include <vector>
30-
31-
#include "CL/sycl.hpp"
32-
#include "oneapi/mkl/stats.hpp"
33-
34-
int main() {
35-
sycl::queue queue;
36-
37-
const size_t n_observations = 1000;
38-
const size_t n_dims = 3;
39-
std::vector<float> x(n_observations * n_dims);
40-
// fill x storage with random numbers
41-
for(int i = 0; i < n_dims, i++) {
42-
for(int j = 0; j < n_observations; j++) {
43-
x[j + i * n_observations] = float(std::rand()) / float(RAND_MAX);
44-
}
45-
}
46-
//create buffer for dataset
47-
sycl::buffer<float, 1> x_buf(x.data(), x.size());
48-
// create buffer for mean values
49-
sycl::buffer<float, 1> mean_buf(n_dims);
50-
// create oneapi::mkl::stats::dataset
51-
auto dataset = oneapi::mkl::stats::make_dataset<oneapi::mkl::stats::layout::row_major>(n_dims, n_observations, x_buf);
52-
53-
54-
oneapi::mkl::stats::mean(queue, dataset, mean_buf);
55-
56-
57-
// create host accessor for mean_buf to print results
58-
auto acc = mean_buf.template get_access<sycl::access::mode::read>();
59-
60-
61-
for(int i = 0; i < n_dims; i++) {
62-
std::cout << "Mean value for dimension " << i << ": " << acc[i] << std::endl;
63-
}
64-
return 0;
65-
}
66-
67-
6823
USM-based example
6924
-----------------
7025

7126
.. code-block:: cpp
7227
73-
#include <iostream>
74-
#include <vector>
75-
76-
#include "CL/sycl.hpp"
7728
#include "oneapi/mkl/stats.hpp"
7829
7930
int main() {
8031
sycl::queue queue;
8132
82-
const size_t n_observations = 1000;
83-
const size_t n_dims = 3;
33+
constexpr std::size_t n_observations = 1000;
34+
constexpr std::size_t n_dims = 3;
8435
85-
sycl::usm_allocator<float, sycl::usm::alloc::shared> allocator(queue);
36+
// allocate Unified Shared Memory for the dataset of the size n_observations * n_dims and fill it with any data
37+
// allocate Unified Shared Memory for the mean output of the size n_dims
8638
87-
std::vector<float, decltype(allocator)> x(n_observations * n_dims, allocator);
88-
// fill x storage with random numbers
89-
for(int i = 0; i < n_dims, i++) {
90-
for(int j = 0; j < n_observations; j++) {
91-
x[j + i * n_observations] = float(std::rand()) / float(RAND_MAX);
92-
}
93-
}
94-
std::vector<float, decltype(allocator)> mean_buf(n_dims, allocator);
9539
// create oneapi::mkl::stats::dataset
96-
auto dataset = oneapi::mkl::stats::make_dataset<oneapi::mkl::stats::layout::row_major>(n_dims, n_observations, x);
97-
98-
sycl::event event = oneapi::mkl::stats::mean(queue, dataset, mean);
99-
event.wait();
100-
for(int i = 0; i < n_dims; i++) {
101-
std::cout << "Mean value for dimension " << i << ": " << mean[i] << std::endl;
102-
}
103-
return 0;
104-
}
40+
auto dataset = oneapi::mkl::stats::make_dataset<oneapi::mkl::stats::layout::row_major>(n_dims, n_observations, dataset_ptr);
10541
42+
// call statistics computation routine
43+
auto event = oneapi::mkl::stats::mean(queue, dataset, mean_ptr);
10644
107-
.. rubric:: USM usage
45+
// wait until computations are completed
46+
event.wait();
10847
109-
You can also use USM with raw pointers by using the sycl::malloc_shared/malloc_device functions.
48+
// ...
49+
}
11050
11151
**Parent topic:** :ref:`onemkl_stats`

0 commit comments

Comments
 (0)