Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 1fb6f00

Browse files
authored
Build dmlc-core with old thread_local implementation (#16526)
1 parent 91bb398 commit 1fb6f00

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ if("$ENV{VERBOSE}" STREQUAL "1")
101101
set(CMAKE_VERBOSE_MAKEFILE ON)
102102
endif()
103103

104+
#Switch off modern thread local for dmlc-core, please see: https://github.com/dmlc/dmlc-core/issues/571#issuecomment-543467484
105+
add_definitions(-DDMLC_MODERN_THREAD_LOCAL=0)
106+
104107

105108
if(MSVC)
106109
add_definitions(-DWIN32_LEAN_AND_MEAN)

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ include $(DMLC_CORE)/make/dmlc.mk
9494
# all tge possible warning tread
9595
WARNFLAGS= -Wall -Wsign-compare
9696
CFLAGS = -DMSHADOW_FORCE_STREAM $(WARNFLAGS)
97+
# use old thread local implementation in DMLC-CORE
98+
CFLAGS += -DDMLC_MODERN_THREAD_LOCAL=0
9799

98100
ifeq ($(DEV), 1)
99101
CFLAGS += -g -Werror

tests/cpp/engine/thread_local_test.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/*!
21+
* Copyright (c) 2019 by Contributors
22+
* \file engine_thread_local_test.cc
23+
* \brief Tests thread safety and lifetime of thread local store
24+
*/
25+
#include <gtest/gtest.h>
26+
#include <time.h>
27+
#include <dmlc/logging.h>
28+
#include <dmlc/thread_group.h>
29+
#include <dmlc/omp.h>
30+
#include <mxnet/c_api.h>
31+
#include <mxnet/engine.h>
32+
#include <mxnet/ndarray.h>
33+
#include <dmlc/timer.h>
34+
#include <cstdio>
35+
#include <thread>
36+
#include <chrono>
37+
#include <vector>
38+
39+
struct A {
40+
std::vector<int> a;
41+
};
42+
int num_threads = 10;
43+
int num_elements = num_threads * 10;
44+
45+
static int ThreadSafetyTest(int num, std::vector<int>* tmp_inputs, std::vector<int*>* res) {
46+
A *ret = dmlc::ThreadLocalStore<A>::Get();
47+
for (size_t i = num * 10; i < num * 10 + 10; ++i) {
48+
(*tmp_inputs)[i] = i;
49+
}
50+
ret->a.clear();
51+
ret->a.reserve(10);
52+
for (size_t i = num * 10; i < num * 10 + 10; ++i) {
53+
ret->a.push_back((*tmp_inputs)[i]);
54+
}
55+
(*res)[num] = dmlc::BeginPtr(ret->a);
56+
return 0;
57+
}
58+
59+
TEST(ThreadLocal, verify_thread_safety) {
60+
std::vector<int> tmp_inputs;
61+
tmp_inputs.resize(num_elements);
62+
std::vector<int*> outputs;
63+
outputs.resize(num_threads);
64+
auto func = [&](int num) {
65+
ThreadSafetyTest(num, &tmp_inputs, &outputs);
66+
};
67+
std::vector<std::thread> worker_threads(num_threads);
68+
int count = 0;
69+
for (auto&& i : worker_threads) {
70+
i = std::thread(func, count);
71+
count++;
72+
}
73+
for (auto&& i : worker_threads) {
74+
i.join();
75+
}
76+
77+
for (size_t i = 0; i < num_elements; i++) {
78+
CHECK(outputs[i/10][i%10] == i);
79+
}
80+
}

0 commit comments

Comments
 (0)