Class: Ikra::Translator::ArrayCommandStructBuilder::SingleStructBuilder

Inherits:
Symbolic::Visitor show all
Defined in:
lib/translator/array_command_struct_builder.rb

Overview

This class builds a struct containing references to input (depending) commands for a certain array command. It is a subclass of [Symbolic::Visitor] but does not traverse the tree. We just take advantage of the double dispatch here.

Instance Method Summary collapse

Methods inherited from Symbolic::Visitor

#visit_array_combine_command, #visit_array_host_section_command, #visit_array_identity_command, #visit_array_index_command, #visit_array_reduce_command, #visit_array_select_command, #visit_array_stencil_command, #visit_array_zip_command, #visit_fixed_size_array_in_host_section_command

Instance Method Details

#struct_name(command) ⇒ Object



87
88
89
# File 'lib/translator/array_command_struct_builder.rb', line 87

def struct_name(command)
    return ArrayCommandStructBuilder.struct_name(command)
end

#visit_array_command(command) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/translator/array_command_struct_builder.rb', line 91

def visit_array_command(command)
    this_name = struct_name(command)
    struct_def = "struct #{this_name} {\n"
    
    # Debug information
    struct_def = struct_def + "    // #{command.class}\n"

    # Generate fields
    struct_def = struct_def + "    #{command.result_type.to_c_type} *result;\n"

    all_params = ["#{command.result_type.to_c_type} *result = NULL"]
    all_initializers = ["result(result)"]

    command.input.each_with_index do |input, index|
        if input.command.is_a?(Symbolic::ArrayCommand)
            struct_def = struct_def + "    #{struct_name(input.command)} *input_#{index};\n"
            all_params.push("#{struct_name(input.command)} *input_#{index} = NULL")
            all_initializers.push("input_#{index}(input_#{index})")
        end
    end

    # Generate constructor
    struct_def = struct_def + "    __host__ __device__ #{this_name}(#{all_params.join(', ')}) : #{all_initializers.join(', ')} { }\n"

    # Add instance methods
    if RequireRuntimeSizeChecker.require_size_function?(command)
        # ArrayIndexCommand does not have any input, as an example. But in this
        # case, we also do not need the `size` function, because it is a root
        # command that can be fused.
        struct_def = struct_def + "    int size() { return input_0->size(); }\n"
    end

    struct_def = struct_def + "};"
end

#visit_array_in_host_section_command(command) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/translator/array_command_struct_builder.rb', line 126

def visit_array_in_host_section_command(command)
    this_name = struct_name(command)
    struct_def = "struct #{this_name} {\n"

    # Debug information
    struct_def = struct_def + "    // #{command.class}\n"
    
    struct_def = struct_def + "    #{command.result_type.to_c_type} *result;\n"
    struct_def = struct_def + "    variable_size_array_t input_0;\n"
    struct_def = struct_def + "    __host__ __device__ #{this_name}(#{command.result_type.to_c_type} *result = NULL, variable_size_array_t input_0 = variable_size_array_t::error_return_value) : result(result), input_0(input_0) { }\n"

    # Add instance methods
    struct_def = struct_def + "    int size() { return input_0.size; }\n"

    return struct_def + "};"
end