This repository was archived by the owner on Feb 2, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathutils.cpp
More file actions
184 lines (156 loc) · 5.13 KB
/
Copy pathutils.cpp
File metadata and controls
184 lines (156 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// *****************************************************************************
// Copyright (c) 2020, Intel Corporation All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// *****************************************************************************
#include "utils.hpp"
#include "tbb/task_arena.h"
#include "tbb/tbb.h"
#include <memory>
#include <iostream>
#include <cmath>
#define HAS_TASK_SCHEDULER_INIT (TBB_INTERFACE_VERSION < 12002)
#define HAS_TASK_SCHEDULER_HANDLE (TBB_INTERFACE_VERSION >= 12003)
namespace utils
{
namespace tbb_control
{
using arena_ptr = std::unique_ptr<tbb::task_arena>;
#if HAS_TASK_SCHEDULER_INIT
using tsi_ptr = std::unique_ptr<tbb::task_scheduler_init>;
void ignore_assertion( const char*, int, const char*, const char * ) {}
#elif HAS_TASK_SCHEDULER_HANDLE
using tsh_t = tbb::task_scheduler_handle;
#else
#pragma message("Unsupported version of TBB. Parallel sorting is disabled")
#endif
struct tbb_context
{
#if HAS_TASK_SCHEDULER_INIT
tsi_ptr tsi;
#elif HAS_TASK_SCHEDULER_HANDLE
tsh_t tsh;
#else
#pragma message("Unsupported version of TBB. Parallel sorting is disabled")
#endif
arena_ptr arena;
tbb_context()
{
#if HAS_TASK_SCHEDULER_INIT
tsi.reset(new tbb::task_scheduler_init(tbb::task_arena::automatic));
#elif HAS_TASK_SCHEDULER_HANDLE
tsh = tbb::task_scheduler_handle::get();
#else
#pragma message("Unsupported version of TBB. Parallel sorting is disabled")
#endif
arena.reset(new tbb::task_arena());
}
void set_threads_num(uint64_t threads)
{
arena->terminate();
arena->initialize(threads);
}
void finalize()
{
if (!arena)
return;
arena->terminate();
arena.reset();
#if HAS_TASK_SCHEDULER_INIT
auto orig = tbb::set_assertion_handler(ignore_assertion);
tsi->terminate(); // no blocking terminate is needed here
tsi.reset();
tbb::set_assertion_handler(orig);
#elif HAS_TASK_SCHEDULER_HANDLE
(void)tbb::finalize(tsh, std::nothrow);
#else
#pragma message("Unsupported version of TBB. Parallel sorting is disabled")
#endif
}
~tbb_context()
{
finalize();
}
};
using tbb_context_ptr = tbb_context*;
tbb_context_ptr& get_tbb_context()
{
static tbb_context_ptr context = nullptr;
return context;
}
void init()
{
auto& ptr = get_tbb_context();
if (ptr)
return;
ptr = new tbb_context();
}
tbb::task_arena& get_arena()
{
auto context = get_tbb_context();
return *context->arena;
}
void set_threads_num(uint64_t threads)
{
auto context = get_tbb_context();
context->set_threads_num(threads);
}
void finalize()
{
auto& context_ptr = get_tbb_context();
context_ptr->finalize();
delete context_ptr;
context_ptr = nullptr;
}
} // tbb_control
void parallel_copy(void* src, void* dst, uint64_t len, uint64_t size)
{
using range_t = tbb::blocked_range<uint64_t>;
tbb::parallel_for(range_t(0,len), [src, dst, size](const range_t& range)
{
auto r_src = reinterpret_cast<quant*>(src) + range.begin()*size;
auto r_dst = reinterpret_cast<quant*>(dst) + range.begin()*size;
std::copy_n(r_src, range.size()*size, r_dst);
});
}
template<>
bool nanless<float>(const float& left, const float& right)
{
return std::less<float>()(left, right) || (std::isnan(right) && !std::isnan(left));
}
template<>
bool nanless<double>(const double& left, const double& right)
{
return std::less<double>()(left, right) || (std::isnan(right) && !std::isnan(left));
}
template<>
bool nangreater<float>(const float& left, const float& right)
{
return std::greater<float>()(left, right) || (std::isnan(right) && !std::isnan(left));
}
template<>
bool nangreater<double>(const double& left, const double& right)
{
return std::greater<double>()(left, right) || (std::isnan(right) && !std::isnan(left));
}
}