Class: Ikra::Translator::ArrayCommandVisitor

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

Overview

A visitor traversing the tree (currently list) of symbolic array commands. Every command is converted into a CommandTranslationResult and possibly merged with the result of dependent (previous) results. This is how kernel fusion is implemented.

Instance Method Summary collapse

Methods inherited from Symbolic::Visitor

#visit_array_command, #visit_array_select_command

Constructor Details

#initialize(environment_builder) ⇒ ArrayCommandVisitor



296
297
298
# File 'lib/translator/command_translator.rb', line 296

def initialize(environment_builder)
    @environment_builder = environment_builder
end

Instance Method Details

#visit_array_identity_command(command) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/translator/command_translator.rb', line 322

def visit_array_identity_command(command)
    # create brand new result
    command_translation_result = CommandTranslationResult.new(@environment_builder)

    # no source code generation

    if Configuration::JOB_REORDERING
        reordering_array = command.target.each_with_index.sort do |a, b|
            a.first.class.object_id <=> b.first.class.object_id
        end.map(&:last)

        # Generate debug output
        dbg_elements = []
        dbg_last = command.target[reordering_array[0]].class
        dbg_counter = 1

        for idx in 1..(command.target.size - 1)
            dbg_next = command.target[reordering_array[idx]].class

            if dbg_next == dbg_last
                dbg_counter += 1
            else
                dbg_elements.push("#{dbg_last.to_s} (#{dbg_counter})")
                dbg_last = dbg_next
                dbg_counter = 1
            end
        end
        dbg_elements.push("#{dbg_last.to_s} (#{dbg_counter})")

        Log.info("Generated job reordering array, resulting in: [#{dbg_elements.join(", ")}]")

        reordering_array_name = @environment_builder.add_base_array("#{command.unique_id}j", reordering_array)
        command_translation_result.invocation = "#{Constants::ENV_IDENTIFIER}->#{EnvironmentBuilder.base_identifier(command.unique_id)}[#{Constants::ENV_IDENTIFIER}->#{reordering_array_name}[threadIdx.x + blockIdx.x * blockDim.x]]"
    else
        command_translation_result.invocation = "#{Constants::ENV_IDENTIFIER}->#{EnvironmentBuilder.base_identifier(command.unique_id)}[threadIdx.x + blockIdx.x * blockDim.x]"
    end
    
    command_translation_result.size = command.size
    command_translation_result.return_type = command.base_type

    command_translation_result
end

#visit_array_map_command(command) ⇒ Object



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'lib/translator/command_translator.rb', line 365

def visit_array_map_command(command)
    dependent_result = super                            # visit target (dependent) command
    command_translation_result = CommandTranslationResult.new(@environment_builder)

    block_translation_result = Translator.translate_block(
        ast: command.ast,
        block_parameter_types: {command.block_parameter_names.first => dependent_result.return_type},
        environment_builder: @environment_builder[command.unique_id],
        lexical_variables: command.lexical_externals,
        command_id: command.unique_id)

    command_translation_result.generated_source = dependent_result.generated_source + "\n\n" + block_translation_result.generated_source

    command_translation_result.invocation = "#{block_translation_result.function_name}(#{Constants::ENV_IDENTIFIER}, #{dependent_result.invocation})"
    command_translation_result.size = dependent_result.size
    command_translation_result.return_type = block_translation_result.result_type

    command_translation_result
end

#visit_array_new_command(command) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/translator/command_translator.rb', line 300

def visit_array_new_command(command)
    # create brand new result
    command_translation_result = CommandTranslationResult.new(@environment_builder)

    block_translation_result = Translator.translate_block(
        ast: command.ast,
        # only one block parameter (int)
        block_parameter_types: {command.block_parameter_names.first => Types::UnionType.create_int},
        environment_builder: @environment_builder[command.unique_id],
        lexical_variables: command.lexical_externals,
        command_id: command.unique_id)

    command_translation_result.generated_source = block_translation_result.generated_source

    tid = "threadIdx.x + blockIdx.x * blockDim.x"
    command_translation_result.invocation = "#{block_translation_result.function_name}(#{Constants::ENV_IDENTIFIER}, #{tid})"
    command_translation_result.size = command.size
    command_translation_result.return_type = block_translation_result.result_type

    command_translation_result
end