semaphore.h
Go to the documentation of this file.
/***************************************************************************************************
* Copyright (c) 2017-2019, NVIDIA 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.
* * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* 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 NVIDIA CORPORATION 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 TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**************************************************************************************************/
#pragma once
#include "cutlass/cutlass.h"
#include "cutlass/aligned_buffer.h"
#include "cutlass/array.h"
#include "cutlass/numeric_types.h"
#include "cutlass/matrix_shape.h"
#include "cutlass/gemm/gemm.h"
namespace cutlass {
class Semaphore {
public:
int *lock;
bool wait_thread;
int state;
public:
CUTLASS_HOST_DEVICE
Semaphore(int *lock_, int thread_id):
lock(lock_),
wait_thread(thread_id < 0 || thread_id == 0),
state(-1) {
}
CUTLASS_DEVICE
void fetch() {
asm volatile ("ld.global.cg.s32 %0, [%1];\n" : "=r"(state) : "l"(lock));
}
CUTLASS_DEVICE
int get_state() const {
return state;
}
CUTLASS_DEVICE
void wait(int status = 0) {
if (wait_thread) {
while (state != status) {
fetch();
__syncwarp(0x01);
};
}
__syncthreads();
}
CUTLASS_DEVICE
void release(int status = 0) {
__syncthreads();
if (wait_thread) {
asm volatile ("st.global.cg.s32 [%0], %1;\n" : : "l"(lock), "r"(status));
}
}
};
} // namespace cutlass
cutlass
Definition: aligned_buffer.h:35
gemm.h
Defines common types used for all GEMM-like operators.
cutlass::Semaphore::Semaphore
CUTLASS_HOST_DEVICE Semaphore(int *lock_, int thread_id)
Implements a semaphore to wait for a flag to reach a given value.
Definition: semaphore.h:59
cutlass::Semaphore::fetch
CUTLASS_DEVICE void fetch()
Permit fetching the synchronization mechanism early.
Definition: semaphore.h:68
array.h
Statically sized array of elements that accommodates all CUTLASS-supported numeric types and is safe ...
matrix_shape.h
Defines a Shape template for matrix tiles.
cutlass::Semaphore::get_state
CUTLASS_DEVICE int get_state() const
Gets the internal state.
Definition: semaphore.h:75
cutlass::Semaphore::wait_thread
bool wait_thread
Definition: semaphore.h:52
aligned_buffer.h
AlignedBuffer is a container for trivially copyable elements suitable for use in unions and shared me...
CUTLASS_HOST_DEVICE
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
numeric_types.h
Top-level include for all CUTLASS numeric types.
cutlass::Semaphore
CTA-wide semaphore for inter-CTA synchronization.
Definition: semaphore.h:48
cutlass::Semaphore::release
CUTLASS_DEVICE void release(int status=0)
Updates the lock with the given result.
Definition: semaphore.h:98
cutlass::Semaphore::wait
CUTLASS_DEVICE void wait(int status=0)
Waits until the semaphore is equal to the given value.
Definition: semaphore.h:81
cutlass.h
Basic include for CUTLASS.
cutlass::Semaphore::state
int state
Definition: semaphore.h:53
cutlass::Semaphore::lock
int * lock
Definition: semaphore.h:51