1 /*******************************************************************************
2 * Copyright 2017-2018 Intel Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *******************************************************************************/
17 #include "mkldnn_thread.hpp"
20 #include "scratchpad.hpp"
25 /* Allocating memory buffers on a page boundary to reduce TLB/page misses */
26 const size_t page_size = 2097152;
29 Implementation of the scratchpad_t interface that is compatible with
30 a concurrent execution
32 struct concurent_scratchpad_t : public scratchpad_t {
33 concurent_scratchpad_t(size_t size) {
35 scratchpad_ = (char *) malloc(size, page_size);
36 assert(scratchpad_ != nullptr);
39 ~concurent_scratchpad_t() {
43 virtual char *get() const {
53 Implementation of the scratchpad_t interface that uses a global
57 struct global_scratchpad_t : public scratchpad_t {
58 global_scratchpad_t(size_t size) {
60 if (scratchpad_ != nullptr) free(scratchpad_);
62 scratchpad_ = (char *) malloc(size, page_size);
63 assert(scratchpad_ != nullptr);
68 ~global_scratchpad_t() {
70 if (reference_count_ == 0) {
72 scratchpad_ = nullptr;
77 virtual char *get() const {
82 thread_local static char *scratchpad_;
83 thread_local static size_t size_;
84 thread_local static unsigned int reference_count_;
87 thread_local char *global_scratchpad_t::scratchpad_ = nullptr;
88 thread_local size_t global_scratchpad_t::size_ = 0;
89 thread_local unsigned int global_scratchpad_t::reference_count_ = 0;
93 Scratchpad creation routine
95 scratchpad_t *create_scratchpad(size_t size) {
96 #ifndef MKLDNN_ENABLE_CONCURRENT_EXEC
97 return new global_scratchpad_t(size);
99 return new concurent_scratchpad_t(size);