Skip to content

Commit 2ae0153

Browse files
Example: time test example for random.normal distr. (#752)
* Example: time test example for random.normal distr.
1 parent ff83dba commit 2ae0153

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

dpnp/backend/examples/example10.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2016-2020, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are met:
7+
// - Redistributions of source code must retain the above copyright notice,
8+
// this list of conditions and the following disclaimer.
9+
// - Redistributions in binary form must reproduce the above copyright notice,
10+
// this list of conditions and the following disclaimer in the documentation
11+
// and/or other materials provided with the distribution.
12+
//
13+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23+
// THE POSSIBILITY OF SUCH DAMAGE.
24+
//*****************************************************************************
25+
26+
/**
27+
* Example 10.
28+
*
29+
* Possible compile line:
30+
* clang++ -fsycl dpnp/backend/examples/example10.cpp -Idpnp -Idpnp/backend/include -Ldpnp -Wl,-rpath='$ORIGIN'/dpnp -ldpnp_backend_c -o example10 -lmkl_sycl -lmkl_intel_ilp64 -lmkl_core
31+
*/
32+
33+
#include <iostream>
34+
#include <time.h>
35+
36+
#include <CL/sycl.hpp>
37+
#include <oneapi/mkl.hpp>
38+
39+
#include <dpnp_iface.hpp>
40+
41+
void test_dpnp_random_normal(
42+
const size_t size, const size_t iters, const size_t seed, const double loc, const double scale)
43+
{
44+
clock_t start, end;
45+
double dev_time_used = 0.0;
46+
double sum_dev_time_used = 0.0;
47+
48+
dpnp_queue_initialize_c(QueueOptions::GPU_SELECTOR);
49+
50+
double* result = (double*)dpnp_memory_alloc_c(size * sizeof(double));
51+
52+
dpnp_rng_srand_c(seed); // TODO: will move
53+
54+
for (size_t i = 0; i < iters; ++i)
55+
{
56+
start = clock();
57+
dpnp_rng_normal_c<double>(result, loc, scale, size);
58+
end = clock();
59+
sum_dev_time_used += ((double)(end - start)) / CLOCKS_PER_SEC;
60+
// TODO: cumulative addition error
61+
// div error
62+
}
63+
64+
dpnp_memory_free_c(result);
65+
66+
dev_time_used = sum_dev_time_used / iters;
67+
std::cout << "DPNPC random normal: " << dev_time_used << std::endl;
68+
69+
return;
70+
}
71+
72+
// TODO: name check
73+
void test_mkl_random_normal(
74+
const size_t size, const size_t iters, const size_t seed, const double loc, const double scale)
75+
{
76+
clock_t start, end;
77+
double dev_time_used = 0.0;
78+
double sum_dev_time_used = 0.0;
79+
80+
cl::sycl::queue queue{cl::sycl::gpu_selector()};
81+
82+
double* result = reinterpret_cast<double*>(malloc_shared(size * sizeof(double), queue));
83+
if (result == nullptr)
84+
{
85+
throw std::runtime_error("Error: out of memory.");
86+
}
87+
88+
oneapi::mkl::rng::mt19937 rng_engine(queue, seed);
89+
oneapi::mkl::rng::gaussian<double> distribution(loc, scale);
90+
91+
for (size_t i = 0; i < iters; ++i)
92+
{
93+
start = clock();
94+
95+
auto event_out = oneapi::mkl::rng::generate(distribution, rng_engine, size, result);
96+
event_out.wait();
97+
98+
end = clock();
99+
100+
sum_dev_time_used += ((double)(end - start)) / CLOCKS_PER_SEC;
101+
// TODO: cumulative addition error
102+
// div error
103+
}
104+
free(result, queue);
105+
106+
std::cout << "MKL time: ";
107+
dev_time_used = sum_dev_time_used / iters;
108+
std::cout << dev_time_used << std::endl;
109+
110+
return;
111+
}
112+
113+
int main(int, char**)
114+
{
115+
const size_t size = 100000000;
116+
const size_t iters = 30;
117+
118+
const size_t seed = 10;
119+
const double loc = 0.0;
120+
const double scale = 1.0;
121+
122+
std::cout << "Normal distr. params:\nloc is " << loc << ", scale is " << scale << std::endl;
123+
124+
test_dpnp_random_normal(size, iters, seed, loc, scale);
125+
test_mkl_random_normal(size, iters, seed, loc, scale);
126+
127+
return 0;
128+
}

0 commit comments

Comments
 (0)