Class: Ikra::Translator::CommandTranslator::KernelBuilder
- Defined in:
- lib/translator/kernel_builder.rb
Overview
Builds a CUDA kernel. This class is responsible for generating the kernel function itself (not the block functions/methods though).
For example: __global__ void kernel(env_t *env, int *result, int previous_1, …) { … }
Instance Attribute Summary collapse
-
#additional_parameters ⇒ Object
Additional Parameters for certain commands that are attached to the kernel.
-
#block_invocation ⇒ Object
A string returning the result of this kernel for one thread.
-
#blocks ⇒ Object
An array of all blocks that should be translated.
-
#cached_results ⇒ Object
IDs of commands that whose results are kept on the GPU.
-
#execution ⇒ Object
A string containing the statements that execute the body of the kernel.
-
#kernel_name ⇒ Object
Returns the value of attribute kernel_name.
-
#methods ⇒ Object
An array of all methods that should be translated.
-
#previous_kernel_input ⇒ Object
Additional parameters that this kernel should accept (to access the result of previous kernels).
-
#result_type ⇒ Object
The result type of this kernel.
Instance Method Summary collapse
-
#add_additional_parameters(parameter) ⇒ Object
Add additional parameters to the kernel function that might be needed for some computations.
-
#add_block(block) ⇒ Object
Adds a block (source code string) to this builder.
-
#add_cached_result(result_id, type) ⇒ Object
Adds a result that has to be kept on GPU.
-
#add_methods(*method) ⇒ Object
Adds one or multiple methods (source code strings) to this builder.
- #add_previous_kernel_parameter(parameter) ⇒ Object
- #assert_ready_to_build ⇒ Object
- #build_blocks ⇒ Object
- #build_kernel ⇒ Object
-
#build_methods ⇒ Object
— Constructor source code —.
-
#initialize ⇒ KernelBuilder
constructor
A new instance of KernelBuilder.
Constructor Details
#initialize ⇒ KernelBuilder
Returns a new instance of KernelBuilder.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/translator/kernel_builder.rb', line 43 def initialize @methods = [] @blocks = [] @previous_kernel_input = [] @block_invocation = nil @num_threads = nil @additional_parameters = [] @kernel_name = "kernel_" + CommandTranslator.next_unique_id.to_s @cached_results = {} @execution = "" end |
Instance Attribute Details
#additional_parameters ⇒ Object
Additional Parameters for certain commands that are attached to the kernel
38 39 40 |
# File 'lib/translator/kernel_builder.rb', line 38 def additional_parameters @additional_parameters end |
#block_invocation ⇒ Object
A string returning the result of this kernel for one thread
29 30 31 |
# File 'lib/translator/kernel_builder.rb', line 29 def block_invocation @block_invocation end |
#blocks ⇒ Object
An array of all blocks that should be translated
20 21 22 |
# File 'lib/translator/kernel_builder.rb', line 20 def blocks @blocks end |
#cached_results ⇒ Object
IDs of commands that whose results are kept on the GPU
41 42 43 |
# File 'lib/translator/kernel_builder.rb', line 41 def cached_results @cached_results end |
#execution ⇒ Object
A string containing the statements that execute the body of the kernel
32 33 34 |
# File 'lib/translator/kernel_builder.rb', line 32 def execution @execution end |
#kernel_name ⇒ Object
Returns the value of attribute kernel_name.
12 13 14 |
# File 'lib/translator/kernel_builder.rb', line 12 def kernel_name @kernel_name end |
#methods ⇒ Object
An array of all methods that should be translated
17 18 19 |
# File 'lib/translator/kernel_builder.rb', line 17 def methods @methods end |
#previous_kernel_input ⇒ Object
Additional parameters that this kernel should accept (to access the result of previous kernels)
24 25 26 |
# File 'lib/translator/kernel_builder.rb', line 24 def previous_kernel_input @previous_kernel_input end |
#result_type ⇒ Object
The result type of this kernel
35 36 37 |
# File 'lib/translator/kernel_builder.rb', line 35 def result_type @result_type end |
Instance Method Details
#add_additional_parameters(parameter) ⇒ Object
Add additional parameters to the kernel function that might be needed for some computations
72 73 74 |
# File 'lib/translator/kernel_builder.rb', line 72 def add_additional_parameters(parameter) @additional_parameters.push(parameter) end |
#add_block(block) ⇒ Object
Adds a block (source code string) to this builder.
63 64 65 |
# File 'lib/translator/kernel_builder.rb', line 63 def add_block(block) @blocks.push(block) end |
#add_cached_result(result_id, type) ⇒ Object
Adds a result that has to be kept on GPU. Therefore additional memory allocations will be made
77 78 79 |
# File 'lib/translator/kernel_builder.rb', line 77 def add_cached_result(result_id, type) @cached_results[result_id] = type end |
#add_methods(*method) ⇒ Object
Adds one or multiple methods (source code strings) to this builder.
58 59 60 |
# File 'lib/translator/kernel_builder.rb', line 58 def add_methods(*method) @methods.push(*method) end |
#add_previous_kernel_parameter(parameter) ⇒ Object
67 68 69 |
# File 'lib/translator/kernel_builder.rb', line 67 def add_previous_kernel_parameter(parameter) @previous_kernel_input.push(parameter) end |
#assert_ready_to_build ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/translator/kernel_builder.rb', line 81 def assert_ready_to_build required_values = [:block_invocation, :result_type] for selector in required_values if send(selector) == nil raise AssertionError.new( "Not ready to build (KernelBuilder): #{selector} is not set") end end end |
#build_blocks ⇒ Object
99 100 101 |
# File 'lib/translator/kernel_builder.rb', line 99 def build_blocks return @blocks.join("\n\n") end |
#build_kernel ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/translator/kernel_builder.rb', line 103 def build_kernel Log.info("Building kernel (num_blocks=#{@blocks.size})") assert_ready_to_build # Build parameters p_env = Constants::ENV_TYPE + " *" + Constants::ENV_IDENTIFIER p_num_threads = Constants::NUM_THREADS_TYPE + " " + Constants::NUM_THREADS_IDENTIFIER p_result = result_type.to_c_type + " *" + Constants::RESULT_IDENTIFIER p_cached_results = cached_results.map do |result_id, type| type.to_c_type + " *" + Constants::RESULT_IDENTIFIER + result_id end cached_results.each do |result_id, type| @execution = execution + "\n" + " " + Constants::RESULT_IDENTIFIER + result_id + "[_tid_] = " + Constants::TEMP_RESULT_IDENTIFIER + result_id + ";" end previous_kernel_params = [] for var in previous_kernel_input previous_kernel_params.push(var.type.to_c_type + " *" + var.name.to_s) end parameters = ([p_env, p_num_threads, p_result] + p_cached_results + previous_kernel_params + additional_parameters).join(", ") # Build kernel return Translator.read_file(file_name: "kernel.cpp", replacements: { "block_invocation" => block_invocation, "execution" => execution, "kernel_name" => kernel_name, "parameters" => parameters, "num_threads" => Constants::NUM_THREADS_IDENTIFIER}) end |
#build_methods ⇒ Object
— Constructor source code —
95 96 97 |
# File 'lib/translator/kernel_builder.rb', line 95 def build_methods return @methods.join("\n\n") end |