Skip to content

Commit 8983189

Browse files
committed
next step in func interface in backend
1 parent f48d0e6 commit 8983189

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed

dpnp/backend/backend_iface_fptr.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+
* This header file is for interface Cython with C++.
28+
* It should not contains any backend specific headers (like SYCL or MKL) because
29+
* all included headers will be exposed in Cython compilation procedure
30+
*
31+
* Also, this file should contains documentation on functions and types
32+
* which are used in the interface
33+
*/
34+
35+
#include <cstring>
36+
#include <map>
37+
#include <stdexcept>
38+
39+
#include <backend/backend_iface_fptr.hpp>
40+
41+
// C function pointer to the C library template functions
42+
typedef void (*custom_math_2in_1out_func_ptr_t)(void*, void*, void*, size_t);
43+
44+
typedef struct custom_math_2in_1out
45+
{
46+
DPNPFuncType return_type; // return type identifier which expected by the `ptr` function
47+
custom_math_2in_1out_func_ptr_t ptr; // # C function pointer
48+
} custom_math_2in_1out_t;
49+
50+
typedef std::map<DPNPFuncType, custom_math_2in_1out_t> map_2p_t;
51+
typedef std::map<DPNPFuncType, map_2p_t> map_1p_t;
52+
53+
// type_T3 result = func_name<T1, T2>()
54+
std::map<DPNPFuncName, map_1p_t> func_map =
55+
{
56+
{ DPNPFuncName::DPNP_FN_ADD,
57+
{ // T1. First template parameter
58+
{ DPNPFuncType::DPNP_FT_INT,
59+
{ // T2. Second template parameter
60+
{ DPNPFuncType::DPNP_FT_INT, { DPNPFuncType::DPNP_FT_INT, &custom_elemwise_add_c<int, int, int> }},
61+
{ DPNPFuncType::DPNP_FT_LONG, { DPNPFuncType::DPNP_FT_LONG, &custom_elemwise_add_c<int, long, long> }},
62+
{ DPNPFuncType::DPNP_FT_FLOAT, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<int, float, double> }},
63+
{ DPNPFuncType::DPNP_FT_DOUBLE, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<int, double, double> }}
64+
}
65+
},
66+
{ DPNPFuncType::DPNP_FT_LONG,
67+
{ // T2. Second template parameter
68+
{ DPNPFuncType::DPNP_FT_INT, { DPNPFuncType::DPNP_FT_LONG, &custom_elemwise_add_c<long, int, long> }},
69+
{ DPNPFuncType::DPNP_FT_LONG, { DPNPFuncType::DPNP_FT_LONG, &custom_elemwise_add_c<long, long, long> }},
70+
{ DPNPFuncType::DPNP_FT_FLOAT, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<long, float, double> }},
71+
{ DPNPFuncType::DPNP_FT_DOUBLE, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<long, double, double> }}
72+
}
73+
},
74+
{ DPNPFuncType::DPNP_FT_FLOAT,
75+
{ // T2. Second template parameter
76+
{ DPNPFuncType::DPNP_FT_INT, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<float, int, double> }},
77+
{ DPNPFuncType::DPNP_FT_LONG, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<float, long, double> }},
78+
{ DPNPFuncType::DPNP_FT_FLOAT, { DPNPFuncType::DPNP_FT_FLOAT, &custom_elemwise_add_c<float, float, float> }},
79+
{ DPNPFuncType::DPNP_FT_DOUBLE, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<float, double, double> }}
80+
}
81+
},
82+
{ DPNPFuncType::DPNP_FT_DOUBLE,
83+
{ // T2. Second template parameter
84+
{ DPNPFuncType::DPNP_FT_INT, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<double, int, double> }},
85+
{ DPNPFuncType::DPNP_FT_LONG, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<double, long, double> }},
86+
{ DPNPFuncType::DPNP_FT_FLOAT, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<double, float, double> }},
87+
{ DPNPFuncType::DPNP_FT_DOUBLE, { DPNPFuncType::DPNP_FT_DOUBLE, &custom_elemwise_add_c<double, double, double> }}
88+
}
89+
}
90+
}
91+
}
92+
};
93+
94+
95+
void* get_dpnp_function_ptr(DPNPFuncName func_name, DPNPFuncType func_type)
96+
{
97+
return nullptr;
98+
}
99+
100+
void* get_backend_function_name(const char *func_name, const char *type_name)
101+
{
102+
/** Implement it in this way to allow easier play with it */
103+
const char *supported_func_name = "dpnp_dot";
104+
const char *supported_type1_name = "double";
105+
const char *supported_type2_name = "float";
106+
const char *supported_type3_name = "long";
107+
const char *supported_type4_name = "int";
108+
109+
/** of coerce it will be converted into std::map later */
110+
if (!strncmp(func_name, supported_func_name, strlen(supported_func_name)))
111+
{
112+
if (!strncmp(type_name, supported_type1_name, strlen(supported_type1_name)))
113+
{
114+
return reinterpret_cast<void*>(mkl_blas_dot_c<double> );
115+
} else if (!strncmp(type_name, supported_type2_name, strlen(supported_type2_name)))
116+
{
117+
return reinterpret_cast<void*>(mkl_blas_dot_c<float> );
118+
} else if (!strncmp(type_name, supported_type3_name, strlen(supported_type3_name)))
119+
{
120+
return reinterpret_cast<void*>(custom_blas_dot_c<long> );
121+
} else if (!strncmp(type_name, supported_type4_name, strlen(supported_type4_name)))
122+
{
123+
return reinterpret_cast<void*>(custom_blas_dot_c<int> );
124+
}
125+
}
126+
127+
throw std::runtime_error("Intel NumPy Error: Unsupported function call");
128+
}

dpnp/backend/backend_iface_fptr.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
enum class DPNPFuncName : uint32_t
6060
{
6161
DPNP_FN_NONE, /**< Very first element of the enumeration */
62+
DPNP_FN_ADD, /**< Used in numpy.add() implementation */
6263
DPNP_FN_DOT, /**< Used in numpy.dot() implementation */
6364
DPNP_FN_LAST /**< The latest element of the enumeration */
6465
};

0 commit comments

Comments
 (0)