Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement <mutex> #187

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: nvrtc
// UNSUPPORTED: pre-sm-70

// <mutex>

// class mutex;

// mutex& operator=(const mutex&) = delete;

#include<cuda/mutex>

template<int scope>
void test() {
cuda::mutex<scope> m0;
cuda::mutex<scope> m1;
m1 = m0;
}

int main(int, char**)
{
test<cuda::thread_scope_system>();
test<cuda::thread_scope_device>();
test<cuda::thread_scope_block>();
test<cuda::thread_scope_thread>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: nvrtc
// UNSUPPORTED: pre-sm-70

// <mutex>

// class mutex;

// mutex(const mutex&) = delete;

#include<cuda/mutex>

template<int scope>
void test() {
cuda::mutex<scope> m0;
cuda::mutex<scope> m1{m0};
}

int main(int, char**)
{
test<cuda::thread_scope_system>();
test<cuda::thread_scope_device>();
test<cuda::thread_scope_block>();
test<cuda::thread_scope_thread>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads
// UNSUPPORTED: pre-sm-70

// <mutex>

// class mutex;

// mutex();

#include<cuda/mutex>
#include<cuda/std/type_traits>

#include "test_macros.h"

int main(int, char**)
{
static_assert(cuda::std::is_nothrow_default_constructible<cuda::mutex<cuda::thread_scope_system>>::value, "");
static_assert(cuda::std::is_nothrow_default_constructible<cuda::mutex<cuda::thread_scope_device>>::value, "");
static_assert(cuda::std::is_nothrow_default_constructible<cuda::mutex<cuda::thread_scope_block>>::value, "");
static_assert(cuda::std::is_nothrow_default_constructible<cuda::mutex<cuda::thread_scope_thread>>::value, "");

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads
// UNSUPPORTED: pre-sm-70

// FLAKY_TEST.

// <mutex>

// class mutex;

// void lock();

#include<cuda/mutex>
#include<cuda/std/cassert>
#include<cuda/std/chrono>

#include "test_macros.h"

#include "cuda_space_selector.h"
#include "heterogeneous_thread_handler.h"

typedef cuda::std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
typedef Clock::duration duration;
typedef cuda::std::chrono::milliseconds ms;
typedef cuda::std::chrono::nanoseconds ns;

template<class Mutex>
__host__ __device__ void f(Mutex* m)
{
time_point t0 = Clock::now();
m->lock();
time_point t1 = Clock::now();
m->unlock();
assert(t1 - t0 >= ms(250));
}

template<class Mutex,
template<typename, typename> typename Selector,
typename Initializer = constructor_initializer>
__host__ __device__ void test() {
Selector<Mutex, Initializer> sel;
SHARED Mutex* m;
m = sel.construct();

heterogeneous_thread_handler handler;
handler.run_on_first_thread(&Mutex::lock, m);
handler.syncthreads();

handler.run_on_second_thread(f<Mutex>, m);
handler.sleep_first_thread(ms(250));
handler.run_on_first_thread(&Mutex::unlock, m);
handler.join_test_thread();
}

int main(int, char**)
{
#ifndef __CUDA_ARCH__
cuda_thread_count = 2;

test<cuda::mutex<cuda::thread_scope_system>, local_memory_selector>();
test<cuda::mutex<cuda::thread_scope_device>, local_memory_selector>();
test<cuda::mutex<cuda::thread_scope_block>, local_memory_selector>();
test<cuda::mutex<cuda::thread_scope_thread>, local_memory_selector>();
#else
test<cuda::mutex<cuda::thread_scope_system>, shared_memory_selector>();
test<cuda::mutex<cuda::thread_scope_device>, shared_memory_selector>();
test<cuda::mutex<cuda::thread_scope_block>, shared_memory_selector>();
test<cuda::mutex<cuda::thread_scope_thread>, shared_memory_selector>();

test<cuda::mutex<cuda::thread_scope_system>, global_memory_selector>();
test<cuda::mutex<cuda::thread_scope_device>, global_memory_selector>();
test<cuda::mutex<cuda::thread_scope_block>, global_memory_selector>();
test<cuda::mutex<cuda::thread_scope_thread>, global_memory_selector>();
#endif

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads
// UNSUPPORTED: pre-sm-70

// FLAKY_TEST.

// <mutex>

// class mutex;

// bool try_lock();

#include<cuda/mutex>
#include<cuda/std/cassert>
#include<cuda/std/chrono>

#include "test_macros.h"

#include "cuda_space_selector.h"
#include "heterogeneous_thread_handler.h"

typedef cuda::std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
typedef Clock::duration duration;
typedef cuda::std::chrono::milliseconds ms;
typedef cuda::std::chrono::nanoseconds ns;

template<class Mutex>
__host__ __device__ void f(Mutex* m)
{
time_point t0 = Clock::now();
assert(!m->try_lock());
assert(!m->try_lock());
assert(!m->try_lock());
while(!m->try_lock())
;
time_point t1 = Clock::now();
m->unlock();
assert(t1 - t0 >= ms(250));
}

template<class Mutex,
template<typename, typename> typename Selector,
typename Initializer = constructor_initializer>
__host__ __device__ void test() {
Selector<Mutex, Initializer> sel;
SHARED Mutex* m;
m = sel.construct();

heterogeneous_thread_handler handler;
handler.run_on_first_thread(&Mutex::lock, m);
handler.syncthreads();

handler.run_on_second_thread(f<Mutex>, m);
handler.sleep_first_thread(ms(250));
handler.run_on_first_thread(&Mutex::unlock, m);
handler.join_test_thread();
}

int main(int, char**)
{
#ifndef __CUDA_ARCH__
cuda_thread_count = 2;

test<cuda::mutex<cuda::thread_scope_system>, local_memory_selector>();
test<cuda::mutex<cuda::thread_scope_device>, local_memory_selector>();
test<cuda::mutex<cuda::thread_scope_block>, local_memory_selector>();
test<cuda::mutex<cuda::thread_scope_thread>, local_memory_selector>();
#else
test<cuda::mutex<cuda::thread_scope_system>, shared_memory_selector>();
test<cuda::mutex<cuda::thread_scope_device>, shared_memory_selector>();
test<cuda::mutex<cuda::thread_scope_block>, shared_memory_selector>();
test<cuda::mutex<cuda::thread_scope_thread>, shared_memory_selector>();

test<cuda::mutex<cuda::thread_scope_system>, global_memory_selector>();
test<cuda::mutex<cuda::thread_scope_device>, global_memory_selector>();
test<cuda::mutex<cuda::thread_scope_block>, global_memory_selector>();
test<cuda::mutex<cuda::thread_scope_thread>, global_memory_selector>();
#endif

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: nvrtc
// UNSUPPORTED: pre-sm-70

// <mutex>

// class timed_mutex;

// timed_mutex& operator=(const timed_mutex&) = delete;

#include<cuda/mutex>

template<int scope>
void test() {
cuda::timed_mutex<scope> m0;
cuda::timed_mutex<scope> m1;
m1 = m0;
}

int main(int, char**)
{
test<cuda::thread_scope_system>();
test<cuda::thread_scope_device>();
test<cuda::thread_scope_block>();
test<cuda::thread_scope_thread>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: nvrtc
// UNSUPPORTED: pre-sm-70

// <mutex>

// class timed_mutex;

// timed_mutex(const timed_mutex&) = delete;

#include<cuda/mutex>

template<int scope>
void test() {
cuda::timed_mutex<scope> m0;
cuda::timed_mutex<scope> m1{m0};
}

int main(int, char**)
{
test<cuda::thread_scope_system>();
test<cuda::thread_scope_device>();
test<cuda::thread_scope_block>();
test<cuda::thread_scope_thread>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2023 NVIDIA CORPORATION & AFFILIATES
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-threads
// UNSUPPORTED: pre-sm-70

// <mutex>

// class timed_mutex;

// timed_mutex();

#include<cuda/mutex>

#include "test_macros.h"

int main(int, char**)
{
static_assert(cuda::std::is_nothrow_default_constructible<cuda::timed_mutex<cuda::thread_scope_system>>::value, "");
static_assert(cuda::std::is_nothrow_default_constructible<cuda::timed_mutex<cuda::thread_scope_device>>::value, "");
static_assert(cuda::std::is_nothrow_default_constructible<cuda::timed_mutex<cuda::thread_scope_block>>::value, "");
static_assert(cuda::std::is_nothrow_default_constructible<cuda::timed_mutex<cuda::thread_scope_thread>>::value, "");

return 0;
}
Loading
Loading