Skip to content

Commit c58679d

Browse files
cianciosacianciosa
authored andcommitted
Inital support for complex metal kernels.
1 parent 36f47f4 commit c58679d

6 files changed

Lines changed: 343 additions & 89 deletions

File tree

‎graph_framework/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ target_compile_definitions (graph_framework
2626
$<$<BOOL:${SHOW_USE_COUNT}>:SHOW_USE_COUNT>
2727
$<$<BOOL:${USE_INDEX_CACHE}>:USE_INDEX_CACHE>
2828
$<IF:$<BOOL:${USE_VERBOSE}>,USE_VERBOSE=true,USE_VERBOSE=false>
29+
$<$<BOOL:${USE_METAL}>:HEADER_DIR="$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>">
2930
)
3031

3132
target_include_directories (graph_framework

‎graph_framework/cuda_context.hpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,11 +962,13 @@ namespace gpu {
962962
source_buffer << "input[i];" << std::endl;
963963
}
964964
source_buffer << " for (size_t index = i + 1024; index < " << size <<"; index += 1024) {" << std::endl;
965+
source_buffer << " sub_max = max(sub_max, ";
965966
if constexpr (jit::complex_scalar<T>) {
966-
source_buffer << " sub_max = max(sub_max, abs(input[index]));" << std::endl;
967+
source_buffer << "abs(input[index]";
967968
} else {
968-
source_buffer << " sub_max = max(sub_max, input[index]);" << std::endl;
969+
source_buffer << "input[index]";
969970
}
971+
source_buffer << ");" << std::endl;
970972
source_buffer << " }" << std::endl;
971973
source_buffer << " __shared__ " << jit::type_to_string<T> () << " thread_max[32];" << std::endl;
972974
source_buffer << " for (int index = 16; index > 0; index /= 2) {" << std::endl;

‎graph_framework/jit.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ namespace jit {
6464
#ifdef USE_CUDA
6565
gpu::cuda_context<T, SAFE_MATH>,
6666
#elif defined(USE_METAL)
67-
gpu::metal_context<SAFE_MATH>,
67+
gpu::metal_context<T, SAFE_MATH>,
6868
#else
6969
gpu::cpu_context<T, SAFE_MATH>,
7070
#endif

‎graph_framework/metal_context.hpp‎

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define metal_context_h
1010

1111
#include <unordered_set>
12+
#include <stdlib.h>
1213

1314
#import <Metal/Metal.h>
1415

@@ -19,9 +20,10 @@ namespace gpu {
1920
//------------------------------------------------------------------------------
2021
/// @brief Class representing a metal gpu context.
2122
///
23+
/// @tparam T Base type of the calculation.
2224
/// @tparam SAFE_MATH Use @ref general_concepts_safe_math operations.
2325
//------------------------------------------------------------------------------
24-
template<bool SAFE_MATH=false>
26+
template<jit::float_scalar T, bool SAFE_MATH=false>
2527
class metal_context {
2628
private:
2729
/// The metal device.
@@ -82,6 +84,7 @@ namespace gpu {
8284
std::vector<std::string> names,
8385
const bool add_reduction=false) {
8486
NSError *error;
87+
setenv("MTL_HEADER_SEARCH_PATHS", HEADER_DIR, 1);
8588
library = [device newLibraryWithSource:[NSString stringWithCString:kernel_source.c_str()
8689
encoding:NSUTF8StringEncoding]
8790
options:compile_options()
@@ -140,16 +143,16 @@ namespace gpu {
140143
std::vector<id<MTLBuffer>> buffers;
141144

142145
const size_t buffer_element_size = sizeof(float);
143-
for (graph::shared_variable<float, SAFE_MATH> &input : inputs) {
146+
for (graph::shared_variable<T, SAFE_MATH> &input : inputs) {
144147
if (!kernel_arguments.contains(input.get())) {
145-
backend::buffer<float> buffer = input->evaluate();
148+
backend::buffer<T> buffer = input->evaluate();
146149
kernel_arguments[input.get()] = [device newBufferWithBytes:buffer.data()
147150
length:buffer.size()*buffer_element_size
148151
options:MTLResourceStorageModeShared];
149152
buffers.push_back(kernel_arguments[input.get()]);
150153
}
151154
}
152-
for (graph::shared_leaf<float, SAFE_MATH> &output : outputs) {
155+
for (graph::shared_leaf<T, SAFE_MATH> &output : outputs) {
153156
if (!kernel_arguments.contains(output.get())) {
154157
kernel_arguments[output.get()] = [device newBufferWithLength:num_rays*sizeof(float)
155158
options:MTLResourceStorageModeShared];
@@ -285,8 +288,8 @@ namespace gpu {
285288
/// @param[in] run Function to run before reduction.
286289
/// @returns A lambda function to run the kernel.
287290
//------------------------------------------------------------------------------
288-
std::function<float(void)> create_max_call(graph::shared_leaf<float, SAFE_MATH> &argument,
289-
std::function<void(void)> run) {
291+
std::function<T(void)> create_max_call(graph::shared_leaf<T, SAFE_MATH> &argument,
292+
std::function<void(void)> run) {
290293
MTLComputePipelineDescriptor *compute = [MTLComputePipelineDescriptor new];
291294
compute.threadGroupSizeIsMultipleOfThreadExecutionWidth = YES;
292295
compute.computeFunction = [library newFunctionWithName:@"max_reduction"];
@@ -364,7 +367,7 @@ namespace gpu {
364367
/// @param[in] nodes Nodes to output.
365368
//------------------------------------------------------------------------------
366369
void print_results(const size_t index,
367-
const graph::output_nodes<float, SAFE_MATH> &nodes) {
370+
const graph::output_nodes<T, SAFE_MATH> &nodes) {
368371
wait();
369372
for (auto &out : nodes) {
370373
std::cout << static_cast<float *> ([kernel_arguments[out.get()] contents])[index] << " ";
@@ -379,10 +382,10 @@ namespace gpu {
379382
/// @param[in] node Node to check the value for.
380383
/// @returns The value at the index.
381384
//------------------------------------------------------------------------------
382-
float check_value(const size_t index,
383-
const graph::shared_leaf<float, SAFE_MATH> &node) {
385+
T check_value(const size_t index,
386+
const graph::shared_leaf<T, SAFE_MATH> &node) {
384387
wait();
385-
return static_cast<float *> ([kernel_arguments[node.get()] contents])[index];
388+
return static_cast<T *> ([kernel_arguments[node.get()] contents])[index];
386389
}
387390

388391
//------------------------------------------------------------------------------
@@ -391,8 +394,8 @@ namespace gpu {
391394
/// @param[in] node Not to copy buffer to.
392395
/// @param[in] source Host side buffer to copy from.
393396
//------------------------------------------------------------------------------
394-
void copy_to_device(graph::shared_leaf<float, SAFE_MATH> node,
395-
float *source) {
397+
void copy_to_device(graph::shared_leaf<T, SAFE_MATH> node,
398+
T *source) {
396399
const size_t size = [kernel_arguments[node.get()] length];
397400
memcpy([kernel_arguments[node.get()] contents],
398401
source, size);
@@ -404,8 +407,8 @@ namespace gpu {
404407
/// @param[in] node Node to copy buffer from.
405408
/// @param[in,out] destination Host side buffer to copy to.
406409
//------------------------------------------------------------------------------
407-
void copy_to_host(graph::shared_leaf<float, SAFE_MATH> node,
408-
float *destination) {
410+
void copy_to_host(graph::shared_leaf<T, SAFE_MATH> node,
411+
T *destination) {
409412
command_buffer = [queue commandBuffer];
410413

411414
[command_buffer commit];
@@ -425,6 +428,10 @@ namespace gpu {
425428
source_buffer << "#include <metal_stdlib>" << std::endl;
426429
source_buffer << "#include <metal_simdgroup>" << std::endl;
427430
source_buffer << "using namespace metal;" << std::endl;
431+
if constexpr (jit::complex_scalar<T>) {
432+
source_buffer << "#define METAL_DEVICE_CODE" << std::endl;
433+
source_buffer << "#include <special_functions.hpp>" << std::endl;
434+
}
428435
}
429436

430437
//------------------------------------------------------------------------------
@@ -585,8 +592,22 @@ namespace gpu {
585592
<< jit::to_string('v', in.get())
586593
<< "[index] = ";
587594
if constexpr (SAFE_MATH) {
588-
source_buffer << "isnan(" << registers[a.get()]
589-
<< ") ? 0.0 : ";
595+
if constexpr (jit::complex_scalar<T>) {
596+
jit::add_type<T> (source_buffer);
597+
source_buffer << " (";
598+
source_buffer << "isnan(real(" << registers[a.get()]
599+
<< ")) ? 0.0 : real(" << registers[a.get()]
600+
<< "), ";
601+
source_buffer << "isnan(imag(" << registers[a.get()]
602+
<< ")) ? 0.0 : imag(" << registers[a.get()]
603+
<< "));" << std::endl;
604+
} else {
605+
source_buffer << "isnan(" << registers[a.get()]
606+
<< ") ? 0.0 : " << registers[a.get()]
607+
<< ";" << std::endl;
608+
}
609+
} else {
610+
source_buffer << registers[a.get()] << ";" << std::endl;
590611
}
591612
source_buffer << registers[a.get()] << ";" << std::endl;
592613
out_registers.insert(out.get());
@@ -603,8 +624,22 @@ namespace gpu {
603624
source_buffer << " " << jit::to_string('o', out.get())
604625
<< "[index] = ";
605626
if constexpr (SAFE_MATH) {
606-
source_buffer << "isnan(" << registers[a.get()]
607-
<< ") ? 0.0 : ";
627+
if constexpr (jit::complex_scalar<T>) {
628+
jit::add_type<T> (source_buffer);
629+
source_buffer << " (";
630+
source_buffer << "isnan(real(" << registers[a.get()]
631+
<< ")) ? 0.0 : real(" << registers[a.get()]
632+
<< "), ";
633+
source_buffer << "isnan(imag(" << registers[a.get()]
634+
<< ")) ? 0.0 : imag(" << registers[a.get()]
635+
<< "));" << std::endl;
636+
} else {
637+
source_buffer << "isnan(" << registers[a.get()]
638+
<< ") ? 0.0 : " << registers[a.get()]
639+
<< ";" << std::endl;
640+
}
641+
} else {
642+
source_buffer << registers[a.get()] << ";" << std::endl;
608643
}
609644
source_buffer << registers[a.get()] << ";" << std::endl;
610645
out_registers.insert(out.get());
@@ -624,15 +659,30 @@ namespace gpu {
624659
const size_t size) {
625660
source_buffer << std::endl;
626661
source_buffer << "kernel void max_reduction(" << std::endl;
627-
source_buffer << " constant float *input [[buffer(0)]]," << std::endl;
628-
source_buffer << " device float *result [[buffer(1)]]," << std::endl;
662+
source_buffer << " constant ";
663+
jit::add_type<T> (source_buffer);
664+
source_buffer << " *input [[buffer(0)]]," << std::endl;
665+
source_buffer << " device ";
666+
jit::add_type<T> (source_buffer);
667+
source_buffer << " *result [[buffer(1)]]," << std::endl;
629668
source_buffer << " uint i [[thread_position_in_grid]]," << std::endl;
630669
source_buffer << " uint j [[simdgroup_index_in_threadgroup]]," << std::endl;
631670
source_buffer << " uint k [[thread_index_in_simdgroup]]) {" << std::endl;
632671
source_buffer << " if (i < " << size << ") {" << std::endl;
633-
source_buffer << " float sub_max = input[i];" << std::endl;
672+
source_buffer << " " << jit::type_to_string<T> () << " sub_max = ";
673+
if constexpr (jit::complex_scalar<T>) {
674+
source_buffer << "abs(input[i]);" << std::endl;
675+
} else {
676+
source_buffer << "input[i];" << std::endl;
677+
}
634678
source_buffer << " for (size_t index = i + 1024; index < " << size <<"; index += 1024) {" << std::endl;
635-
source_buffer << " sub_max = max(sub_max, input[index]);" << std::endl;
679+
source_buffer << " sub_max = max(sub_max, ";
680+
if constexpr (jit::complex_scalar<T>) {
681+
source_buffer << "abs(input[index]";
682+
} else {
683+
source_buffer << "input[index]";
684+
}
685+
source_buffer << ");" << std::endl;
636686
source_buffer << " }" << std::endl;
637687
source_buffer << " threadgroup float thread_max[32];" << std::endl;
638688
source_buffer << " thread_max[j] = simd_max(sub_max);" << std::endl;
@@ -649,8 +699,8 @@ namespace gpu {
649699
///
650700
/// @param[in] node Node to get the buffer for.
651701
//------------------------------------------------------------------------------
652-
float *get_buffer(graph::shared_leaf<float, SAFE_MATH> &node) {
653-
return static_cast<float *> ([kernel_arguments[node.get()] contents]);
702+
T *get_buffer(graph::shared_leaf<T, SAFE_MATH> &node) {
703+
return static_cast<T *> ([kernel_arguments[node.get()] contents]);
654704
}
655705
};
656706
}

0 commit comments

Comments
 (0)