Method: BOAST::CKernel#build

Defined in:
lib/BOAST/CKernel.rb

#build(options = {}) ⇒ Object



363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/BOAST/CKernel.rb', line 363

def build(options = {})
  compiler_options = BOAST::get_compiler_options
  compiler_options.update(options)
  return build_opencl(compiler_options) if @lang == BOAST::CL
  ldflags = self.setup_compiler(compiler_options)
  extension = ".c" if @lang == BOAST::C
  extension = ".cu" if @lang == BOAST::CUDA
  extension = ".f90" if @lang == BOAST::FORTRAN
#temporary
  c_compiler = compiler_options[:CC]
  c_compiler = "cc" if not c_compiler
  linker = compiler_options[:LD]
  linker = c_compiler if not linker
#end temporary
  if options[:openmp] then
    openmp_ld_flags = BOAST::get_openmp_flags[linker]
      if not openmp_ld_flags then
        keys = BOAST::get_openmp_flags.keys
        keys.each { |k|
          openmp_ld_flags = BOAST::get_openmp_flags[k] if linker.match(k)
        }
      end
      raise "unkwown openmp flags for: #{linker}" if not openmp_ld_flags
      ldflags += " #{openmp_ld_flags}"
  end
  source_file = Tempfile::new([@procedure.name,extension])
  path = source_file.path
  target = path.chomp(File::extname(path))+".o"
  fill_code(source_file)
  source_file.close

  previous_lang = BOAST::get_lang
  previous_output = BOAST::get_output
  BOAST::set_lang(BOAST::C)
  module_file_name = File::split(path.chomp(File::extname(path)))[0] + "/Mod_" + File::split(path.chomp(File::extname(path)))[1].gsub("-","_") + ".c"
  module_name = File::split(module_file_name.chomp(File::extname(module_file_name)))[1]
  module_file = File::open(module_file_name,"w+")
  BOAST::set_output(module_file)
  fill_module(module_file, module_name)
  module_file.rewind
#     puts module_file.read
  module_file.close
  BOAST::set_lang(previous_lang)
  BOAST::set_output(previous_output)
  module_target = module_file_name.chomp(File::extname(module_file_name))+".o"
  module_final = module_file_name.chomp(File::extname(module_file_name))+".so"
  kernel_files = []
  @kernels.each { |kernel|
    kernel_file = Tempfile::new([kernel.procedure.name,".o"])
    kernel.binary.rewind
    kernel_file.write( kernel.binary.read )
    kernel_file.close
    kernel_files.push(kernel_file)
  }
  file module_final => [module_target, target] do
    #puts "#{linker} -shared -o #{module_final} #{module_target} #{target} #{kernel_files.join(" ")} -Wl,-Bsymbolic-functions -Wl,-z,relro -rdynamic -Wl,-export-dynamic #{ldflags}"
    sh "#{linker} -shared -o #{module_final} #{module_target} #{target} #{(kernel_files.collect {|f| f.path}).join(" ")} -Wl,-Bsymbolic-functions -Wl,-z,relro -rdynamic -Wl,-export-dynamic #{ldflags}"
  end
  Rake::Task[module_final].invoke
  require(module_final)
  eval "self.extend(#{module_name})"
  f = File::open(target,"rb")
  @binary = StringIO::new
  @binary.write( f.read )
  f.close
  File.unlink(target)
  File.unlink(module_target)
  File.unlink(module_file_name)
  File.unlink(module_final)
  kernel_files.each { |f|
    f.unlink
  }
  return self
end