Class: Ikra::Translator::CommandTranslator::HostSectionProgramBuilder

Inherits:
ProgramBuilder show all
Defined in:
lib/translator/host_section/program_builder.rb

Instance Attribute Summary collapse

Attributes inherited from ProgramBuilder

#array_command_structs, #environment_builder, #kernel_launchers, #kernels, #root_command, #structs

Instance Method Summary collapse

Methods inherited from ProgramBuilder

#add_array_command_struct, #build_kernel_launchers, #execute

Constructor Details

#initialize(environment_builder:, root_command:) ⇒ HostSectionProgramBuilder

Returns a new instance of HostSectionProgramBuilder.



19
20
21
22
23
# File 'lib/translator/host_section/program_builder.rb', line 19

def initialize(environment_builder:, root_command:)
    super

    @kernel_builders = Set.new
end

Instance Attribute Details

#host_result_expressionObject

An expression that returns the final result, as an ‘variable_size_array_t` object pointing to an array in the host memory.



17
18
19
# File 'lib/translator/host_section/program_builder.rb', line 17

def host_result_expression
  @host_result_expression
end

#host_section_sourceObject

A host C++ function containing the source code of the host section.



10
11
12
# File 'lib/translator/host_section/program_builder.rb', line 10

def host_section_source
  @host_section_source
end

#result_typeObject

The type of the result (not an array type, just the inner type).



13
14
15
# File 'lib/translator/host_section/program_builder.rb', line 13

def result_type
  @result_type
end

Instance Method Details

#add_kernel_launcher(launcher) ⇒ Object



43
44
45
46
47
48
# File 'lib/translator/host_section/program_builder.rb', line 43

def add_kernel_launcher(launcher)
    super

    # Let's keep track of kernels here by ourselves
    @kernel_builders.merge(launcher.kernel_builders)
end

#all_kernel_buildersObject



50
51
52
# File 'lib/translator/host_section/program_builder.rb', line 50

def all_kernel_builders
    return @kernel_builders
end

#assert_ready_to_buildObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/translator/host_section/program_builder.rb', line 25

def assert_ready_to_build
    if host_section_source == nil
        raise AssertionError.new("Not ready to build (HostSectionProgramBuilder): No host section source code defined")
    end

    if result_type == nil
        raise AssertionError.new("Not ready to build (HostSectionProgramBuilder): No result type defined")
    end

    if host_result_expression == nil
        raise AssertionError.new("Not ready to build (HostSectionProgramBuilder): No host result expression defined")
    end
end

#build_memory_free_except_lastObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/translator/host_section/program_builder.rb', line 60

def build_memory_free_except_last
    result = ""

    for launcher in kernel_launchers[0...-1]
        if !launcher.reuse_memory?
            result = result + launcher.build_device_memory_free_in_host_section
        end
    end

    return result
end

#build_programObject

Builds the CUDA program. Returns the source code string.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/translator/host_section/program_builder.rb', line 73

def build_program
    assert_ready_to_build

    result = build_header + build_struct_types + build_header_structs + 
        build_array_command_struct_types + build_environment_struct + 
        build_kernels + host_section_source

    # Build program entry point
    return result + Translator.read_file(file_name: "host_section_entry_point.cpp", replacements: {
        "prepare_environment" => environment_builder.build_environment_variable,
        "host_env_var_name" => Constants::ENV_HOST_IDENTIFIER,
        "host_result_array" => host_result_expression})
end

#clear_kernel_launchersObject



39
40
41
# File 'lib/translator/host_section/program_builder.rb', line 39

def clear_kernel_launchers
    @kernel_launchers.clear
end

#prepare_additional_args_for_launch(command) ⇒ Object



54
55
56
57
58
# File 'lib/translator/host_section/program_builder.rb', line 54

def prepare_additional_args_for_launch(command)
    kernel_launchers.each do |launcher|
        launcher.prepare_additional_args_for_launch(command)
    end
end