CUTLASS
CUDA Templates for Linear Algebra Subroutines and Solvers
semaphore.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  **************************************************************************************************/
29 #pragma once
30 
31 #include "cutlass/cutlass.h"
32 
33 #include "cutlass/aligned_buffer.h"
34 #include "cutlass/array.h"
35 
36 #include "cutlass/numeric_types.h"
37 #include "cutlass/matrix_shape.h"
38 
39 #include "cutlass/gemm/gemm.h"
40 
42 
43 namespace cutlass {
44 
46 
48 class Semaphore {
49 public:
50 
51  int *lock;
53  int state;
54 
55 public:
56 
59  Semaphore(int *lock_, int thread_id):
60  lock(lock_),
61  wait_thread(thread_id < 0 || thread_id == 0),
62  state(-1) {
63 
64  }
65 
67  CUTLASS_DEVICE
68  void fetch() {
69 
70  asm volatile ("ld.global.cg.s32 %0, [%1];\n" : "=r"(state) : "l"(lock));
71  }
72 
74  CUTLASS_DEVICE
75  int get_state() const {
76  return state;
77  }
78 
80  CUTLASS_DEVICE
81  void wait(int status = 0) {
82 
83  if (wait_thread) {
84  while (state != status) {
85 
86  fetch();
87 
88  __syncwarp(0x01);
89 
90  };
91  }
92 
93  __syncthreads();
94  }
95 
97  CUTLASS_DEVICE
98  void release(int status = 0) {
99  __syncthreads();
100 
101  if (wait_thread) {
102 
103  asm volatile ("st.global.cg.s32 [%0], %1;\n" : : "l"(lock), "r"(status));
104  }
105  }
106 };
107 
109 
110 } // namespace cutlass
111 
Definition: aligned_buffer.h:35
Defines common types used for all GEMM-like operators.
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_DEVICE void fetch()
Permit fetching the synchronization mechanism early.
Definition: semaphore.h:68
Statically sized array of elements that accommodates all CUTLASS-supported numeric types and is safe ...
Defines a Shape template for matrix tiles.
CUTLASS_DEVICE int get_state() const
Gets the internal state.
Definition: semaphore.h:75
bool wait_thread
Definition: semaphore.h:52
AlignedBuffer is a container for trivially copyable elements suitable for use in unions and shared me...
#define CUTLASS_HOST_DEVICE
Definition: cutlass.h:89
Top-level include for all CUTLASS numeric types.
CTA-wide semaphore for inter-CTA synchronization.
Definition: semaphore.h:48
CUTLASS_DEVICE void release(int status=0)
Updates the lock with the given result.
Definition: semaphore.h:98
CUTLASS_DEVICE void wait(int status=0)
Waits until the semaphore is equal to the given value.
Definition: semaphore.h:81
Basic include for CUTLASS.
int state
Definition: semaphore.h:53
int * lock
Definition: semaphore.h:51