2019-11-13 15:15:18 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <execution/Ticket.h>
|
|
|
|
#include <execution/ThreadPool.h>
|
|
|
|
#include <helpers/logger.h>
|
|
|
|
#include <array>
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
namespace samediff {
|
2019-11-13 15:15:18 +01:00
|
|
|
Ticket::Ticket(const std::vector<BlockingQueue<CallableWithArguments*>*> &queues) {
|
|
|
|
_acquired = true;
|
|
|
|
_queues = queues;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ticket::Ticket() {
|
|
|
|
_acquired = true;
|
2020-03-02 10:49:41 +01:00
|
|
|
_interfaces.resize(sd::Environment::getInstance()->maxThreads());
|
2019-11-13 15:15:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Ticket::acquired() {
|
|
|
|
return _acquired;
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
void Ticket::enqueue(int thread_id, samediff::CallableWithArguments *callable) {
|
2019-11-13 15:15:18 +01:00
|
|
|
_queues[thread_id]->put(callable);
|
|
|
|
_callables.emplace_back(callable);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::enqueue(uint32_t thread_id, uint32_t num_threads, FUNC_DO func) {
|
|
|
|
_interfaces[thread_id]->fill(thread_id, num_threads, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::enqueue(uint32_t thread_id, uint32_t num_threads, FUNC_1D func, int64_t start_x, int64_t stop_x, int64_t inc_x) {
|
|
|
|
_interfaces[thread_id]->fill(thread_id, num_threads, func, start_x, stop_x, inc_x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::enqueue(uint32_t thread_id, uint32_t num_threads, int64_t *lpt, FUNC_RL func, int64_t start_x, int64_t stop_x, int64_t inc_x) {
|
|
|
|
_interfaces[thread_id]->fill(thread_id, num_threads, lpt, func, start_x, stop_x, inc_x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::enqueue(uint32_t thread_id, uint32_t num_threads, double *dpt, FUNC_RD func, int64_t start_x, int64_t stop_x, int64_t inc_x) {
|
|
|
|
_interfaces[thread_id]->fill(thread_id, num_threads, dpt, func, start_x, stop_x, inc_x);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::enqueue(uint32_t thread_id, uint32_t num_threads, FUNC_2D func, int64_t start_x, int64_t stop_x, int64_t inc_x, int64_t start_y, int64_t stop_y, int64_t inc_y) {
|
|
|
|
_interfaces[thread_id]->fill(thread_id, num_threads, std::move(func), start_x, stop_x, inc_x, start_y, stop_y, inc_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::enqueue(uint32_t thread_id, uint32_t num_threads, FUNC_3D func, int64_t start_x, int64_t stop_x, int64_t inc_x, int64_t start_y, int64_t stop_y, int64_t inc_y, int64_t start_z, int64_t stop_z, int64_t inc_z) {
|
|
|
|
_interfaces[thread_id]->fill(thread_id, num_threads, func, start_x, stop_x, inc_x, start_y, stop_y, inc_y, start_z, stop_z, inc_z);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::acquiredThreads(uint32_t threads) {
|
|
|
|
_acquiredThreads = threads;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Ticket::waitAndRelease() {
|
|
|
|
for (uint32_t e = 0; e < this->_acquiredThreads; e++) {
|
|
|
|
// block until finished
|
|
|
|
_interfaces[e]->waitForCompletion();
|
|
|
|
|
|
|
|
// mark available
|
|
|
|
_interfaces[e]->markAvailable();
|
|
|
|
|
|
|
|
// increment availability counter
|
|
|
|
ThreadPool::getInstance()->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return this ticket back to the pool
|
|
|
|
ThreadPool::getInstance()->release(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
void Ticket::attach(uint32_t thread_id, samediff::CallableInterface *interface) {
|
2019-11-13 15:15:18 +01:00
|
|
|
_interfaces[thread_id] = interface;
|
|
|
|
}
|
|
|
|
}
|