Skip to content

Commit

Permalink
add daal examples in bazel
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandr-Solovev committed Aug 26, 2024
1 parent 32fd9cf commit 060e241
Show file tree
Hide file tree
Showing 14 changed files with 150 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ Status KMeansInitStep3MasterKernel<method, algorithmFPType, cpu>::init(const Par
auto engineImpl = dynamic_cast<daal::algorithms::engines::internal::BatchBaseImpl *>(&engine);
Status s;
DAAL_CHECK(engineImpl, ErrorIncorrectEngineParameter);

DAAL_CHECK_STATUS(
s, (UniformKernelDefault<algorithmFPType, cpu>::compute(algorithmFPType(0.), algorithmFPType(1.), *engineImpl, 1, &dummy)));
_rngState->reserve(engineImpl->getStateSize());
Expand Down
16 changes: 14 additions & 2 deletions cpp/daal/src/externals/service_rng_mkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <mkl.h>
#include "src/externals/service_stat_rng_mkl.h"
#include "src/externals/service_rng_common.h"
#include <iostream>

// RNGs
#define __DAAL_BRNG_MT2203 VSL_BRNG_MT2203
Expand All @@ -52,6 +53,7 @@ int uniformRNG(const size_t n, T * r, void * stream, const T a, const T b, const
template <CpuType cpu>
int uniformRNG(const size_t cn, size_t * r, void * stream, const size_t a, const size_t b, const int method)
{
std::cout << "uniform 1" << std::endl;
size_t n = cn;
int errcode = 0;

Expand Down Expand Up @@ -120,7 +122,8 @@ int uniformRNG(const size_t cn, size_t * r, void * stream, const size_t a, const
for (int i = 0; i < 64; i++) dv /= 2.0;
int nn = (int)n;
unsigned __int64 * rr = cr;
__DAAL_VSLFN_CALL_NR_WHILE(fpk_vsl_kernel, iRngUniformBits64, ((const MKL_INT)method, stream, (const MKL_INT)nn, (unsigned MKL_INT64 *)rr), errcode);
__DAAL_VSLFN_CALL_NR_WHILE(fpk_vsl_kernel, iRngUniformBits64,
((const MKL_INT)method, stream, (const MKL_INT)nn, (unsigned MKL_INT64 *)rr), errcode);

if (errcode != 0)
{
Expand All @@ -136,7 +139,8 @@ int uniformRNG(const size_t cn, size_t * r, void * stream, const size_t a, const
n = cn - pos;
int nn = (int)n;
unsigned __int64 * rr = cr + pos;
__DAAL_VSLFN_CALL_NR_WHILE(fpk_vsl_kernel, iRngUniformBits64, ((const MKL_INT)method, stream, (const MKL_INT)nn, (unsigned MKL_INT64 *)rr), errcode);
__DAAL_VSLFN_CALL_NR_WHILE(fpk_vsl_kernel, iRngUniformBits64,
((const MKL_INT)method, stream, (const MKL_INT)nn, (unsigned MKL_INT64 *)rr), errcode);

if (errcode != 0)
{
Expand Down Expand Up @@ -166,6 +170,7 @@ int uniformRNG(const size_t cn, size_t * r, void * stream, const size_t a, const
template <CpuType cpu>
int uniformRNG(const size_t n, int * r, void * stream, const int a, const int b, const int method)
{
std::cout << "uniform 2" << std::endl;
int errcode = 0;
int nn = (int)n;
int * rr = r;
Expand All @@ -176,6 +181,7 @@ int uniformRNG(const size_t n, int * r, void * stream, const int a, const int b,
template <CpuType cpu>
int uniformRNG(const size_t n, float * r, void * stream, const float a, const float b, const int method)
{
std::cout << "uniform 3" << std::endl;
int errcode = 0;
int nn = (int)n;
float * rr = r;
Expand All @@ -186,6 +192,7 @@ int uniformRNG(const size_t n, float * r, void * stream, const float a, const fl
template <CpuType cpu>
int uniformRNG(const size_t n, double * r, void * stream, const double a, const double b, const int method)
{
std::cout << "uniform 4" << std::endl;
int errcode = 0;
int nn = (int)n;
double * rr = r;
Expand All @@ -196,6 +203,7 @@ int uniformRNG(const size_t n, double * r, void * stream, const double a, const
template <CpuType cpu>
int uniformBits32RNG(const size_t n, unsigned int * r, void * stream, const int method)
{
std::cout << "uniform 5" << std::endl;
int errcode = 0;
int nn = (int)n;
unsigned int * rr = r;
Expand Down Expand Up @@ -298,15 +306,19 @@ class BaseRNG : public BaseRNGIface<cpu>

int getStateSize() const
{
std::cout << "here get state size" << std::endl;
int res = 0;
__DAAL_VSLFN_CALL_NR(fpk_vsl_sub_kernel, vslGetStreamSize, (_stream), res);
std::cout << "here end get state size" << std::endl;
return res;
}

int saveState(void * dest) const
{
std::cout << "here save state" << std::endl;
int errcode = 0;
__DAAL_VSLFN_CALL_NR(fpk_vsl_sub_kernel, vslSaveStreamM, (_stream, (char *)dest), errcode);
std::cout << "here end save state" << std::endl;
return errcode;
}

Expand Down
6 changes: 6 additions & 0 deletions examples/daal/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package(default_visibility = ["//visibility:public"])

filegroup(
name = "data_daal",
srcs = glob(["data/**/*.csv"]),
)
86 changes: 86 additions & 0 deletions examples/daal/cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
load("@onedal//dev/bazel:dal.bzl",
"dal_module",
"dal_algo_example_suite",
)

_DATA_DEPS = [
"@onedal//examples/daal:data_daal",
]

dal_module(
name = "utils",
hdrs = glob(["source/utils/*.h"]),
includes = [ "source",
"source/utils" ],
)

_TEST_DEPS = [
":utils",
]

# dal_example_suite(
# name = "graph",
# compile_as = [ "c++" ],
# srcs = glob(["source/graph/*.cpp"]),
# dal_deps = [
# "@onedal//cpp/oneapi/dal/graph",
# ],
# data = _DATA_DEPS,
# extra_deps = _TEST_DEPS,
# )

# dal_example_suite(
# name = "table",
# compile_as = [ "c++" ],
# srcs = glob(["source/table/*.cpp"]),
# dal_deps = [
# "@onedal//cpp/oneapi/dal/table",
# ],
# data = _DATA_DEPS,
# extra_deps = _TEST_DEPS,
# )

# dal_example_suite(
# name = "kmeans_init",
# compile_as = [ "c++" ],
# srcs = glob(["source/kmeans_init/*.cpp"]),
# dal_deps = [
# "@onedal//cpp/oneapi/dal/algo:kmeans",
# "@onedal//cpp/oneapi/dal/algo:kmeans_init",
# ],
# data = _DATA_DEPS,
# extra_deps = _TEST_DEPS,
# )

# dal_example_suite(
# name = "misc",
# compile_as = [ "c++" ],
# srcs = glob(["source/misc/*.cpp"]),
# dal_deps = [
# "@onedal//cpp/oneapi/dal:io",
# ],
# data = _DATA_DEPS,
# extra_deps = _TEST_DEPS,
# )

# dal_example_suite(
# name = "jaccard",
# compile_as = [ "c++" ],
# srcs = glob(["source/jaccard/*.cpp"]),
# dal_deps = [
# "@onedal//cpp/oneapi/dal/algo:jaccard",
# ],
# data = _DATA_DEPS,
# extra_deps = _TEST_DEPS + [
# "@tbb//:tbb",
# ],
# )

dal_algo_example_suite(
algos = [
"kmeans",
],
compile_as = [ "c++" ],
data = _DATA_DEPS,
extra_deps = _TEST_DEPS,
)
2 changes: 1 addition & 1 deletion examples/daal/cpp/source/kmeans/kmeans_csr_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main(int argc, char* argv[]) {
checkArguments(argc, argv, 1, &datasetFileName);

/* Retrieve the data from the input file */
CSRNumericTablePtr dataTable(createSparseTable<float>(datasetFileName));
CSRNumericTablePtr dataTable(createSparseTable<float>(get_data_path(datasetFileName)));

/* Get initial clusters for the K-Means algorithm */
kmeans::init::Batch<algorithmFPType, kmeans::init::randomCSR> init(nClusters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ int main(int argc, char* argv[]) {
checkArguments(argc, argv, 1, &datasetFileName);

/* Retrieve the data from the input file */
CSRNumericTablePtr dataTable(createSparseTable<float>(datasetFileName));
CSRNumericTablePtr dataTable(createSparseTable<float>(get_data_path(datasetFileName)));

/* Get initial clusters for the K-Means algorithm */
kmeans::init::Batch<algorithmFPType, kmeans::init::randomCSR> init(nClusters);
Expand Down
3 changes: 2 additions & 1 deletion examples/daal/cpp/source/kmeans/kmeans_csr_distr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ int main(int argc, char* argv[]) {
nClusters);
for (size_t i = 0; i < nBlocks; i++) {
/* Read dataFileNames and create a numeric table to store the input data */
dataTable[i] = CSRNumericTablePtr(createSparseTable<float>(dataFileNames[i]));
dataTable[i] =
CSRNumericTablePtr(createSparseTable<float>(get_data_path(dataFileNames[i])));

/* Create an algorithm object for the K-Means algorithm */
kmeans::init::Distributed<step1Local, algorithmFPType, kmeans::init::randomCSR> localInit(
Expand Down
2 changes: 1 addition & 1 deletion examples/daal/cpp/source/kmeans/kmeans_dense_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int argc, char* argv[]) {
checkArguments(argc, argv, 1, &datasetFileName);

/* Initialize FileDataSource to retrieve the input data from a .csv file */
FileDataSource<CSVFeatureManager> dataSource(datasetFileName,
FileDataSource<CSVFeatureManager> dataSource(get_data_path(datasetFileName),
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ int main(int argc, char* argv[]) {
checkArguments(argc, argv, 1, &datasetFileName);

/* Initialize FileDataSource to retrieve the input data from a .csv file */
FileDataSource<CSVFeatureManager> dataSource(datasetFileName,
FileDataSource<CSVFeatureManager> dataSource(get_data_path(datasetFileName),
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);

Expand Down
4 changes: 2 additions & 2 deletions examples/daal/cpp/source/kmeans/kmeans_dense_distr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "daal.h"
#include "service.h"

#include <iostream>
using namespace daal;
using namespace daal::algorithms;
using namespace daal::data_management;
Expand Down Expand Up @@ -66,7 +66,7 @@ int main(int argc, char* argv[]) {
nClusters);
for (size_t i = 0; i < nBlocks; i++) {
/* Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file */
FileDataSource<CSVFeatureManager> dataSource(dataFileNames[i],
FileDataSource<CSVFeatureManager> dataSource(get_data_path(dataFileNames[i]),
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);

Expand Down
2 changes: 1 addition & 1 deletion examples/daal/cpp/source/kmeans/kmeans_init_csr_distr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const std::string dataFileNames[] = { "../data/distributed/kmeans_csr_1.csv",

void loadData(NumericTablePtr data[nBlocks]) {
for (size_t i = 0; i < nBlocks; i++)
data[i] = CSRNumericTablePtr(createSparseTable<float>(dataFileNames[i]));
data[i] = CSRNumericTablePtr(createSparseTable<float>(get_data_path(dataFileNames[i])));
}

template <kmeans::init::Method method>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int main(int argc, char* argv[]) {
checkArguments(argc, argv, 1, &datasetFileName);

/* Initialize FileDataSource to retrieve the input data from a .csv file */
FileDataSource<CSVFeatureManager> dataSource(datasetFileName,
FileDataSource<CSVFeatureManager> dataSource(get_data_path(datasetFileName),
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const std::string dataFileNames[] = { "../data/distributed/kmeans_dense_1.csv",
void loadData(NumericTablePtr data[nBlocks]) {
for (size_t i = 0; i < nBlocks; i++) {
/* Initialize FileDataSource<CSVFeatureManager> to retrieve the input data from a .csv file */
FileDataSource<CSVFeatureManager> dataSource(dataFileNames[i],
FileDataSource<CSVFeatureManager> dataSource(get_data_path(dataFileNames[i]),
DataSource::doAllocateNumericTable,
DataSource::doDictionaryFromContext);

Expand Down
41 changes: 32 additions & 9 deletions examples/daal/cpp/source/utils/service.h
Original file line number Diff line number Diff line change
Expand Up @@ -658,18 +658,41 @@ void printNumericTables(daal::data_management::NumericTablePtr dataTable1,
interval);
}

bool checkFileIsAvailable(std::string filename, bool needExit = false) {
std::ifstream file(filename.c_str());
if (file.good()) {
return true;
inline bool check_file(const std::string &name) {
return std::ifstream{ name }.good();
}

inline std::string get_data_path(const std::string &name) {
const std::vector<std::string> paths = { "../data/batch",
"examples/daal/data/batch",
"examples/daal/data/online",
"examples/daal/data/distributed",
"examples/daal/cpp" };

for (const auto &path : paths) {
const std::string try_path = path + "/" + name;
if (check_file(try_path)) {
return try_path;
}
}
else {
std::cout << "Can't open file " << filename << std::endl;
if (needExit) {
exit(fileError);
return name;
}

bool checkFileIsAvailable(std::string name, bool needExit = false) {
const std::vector<std::string> paths = { "../data/batch",
"examples/daal/data/batch",
"examples/daal/data/online",
"examples/daal/data/distributed",
"examples/daal/cpp" };
for (const auto &path : paths) {
const std::string try_path = path + "/" + name;
if (check_file(try_path)) {
return check_file(try_path);
}
return false;
}
throw std::runtime_error(std::string("Failed: ") + name);

return false;
}

void checkArguments(int argc, char *argv[], int count, ...) {
Expand Down

0 comments on commit 060e241

Please sign in to comment.