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