Esempio n. 1
0
    def _generate_cu(self):
        cu_gen = CUDAGenerator('{}/code_gen/cudamemop.cu'.format(pkg_dir))

        cu_gen.blank_line(2)
        cu_gen.include("../c_code/helper_cuda.h")
        cu_gen.include("cudamemop.h")
        cu_gen.blank_line(2)

        for i in self.types:
            cu_gen.line_no_end("{}* gpu_malloc_{}(int size)".format(i, i), 0)
            cu_gen.open_brace(0)
            cu_gen.line('{} * gpu = NULL'.format(i))
            cu_gen.malloc_gpu(ret='gpu', type_=str(i), num='size')
            cu_gen.line(line='return gpu')
            cu_gen.close_brace(0)
            cu_gen.blank_line()

            cu_gen.line_no_end(
                "void cpu2gpu_{}({} *cpu, {} *gpu, int size)".format(i, i, i),
                0)
            cu_gen.open_brace(0)
            cu_gen.cpu_to_gpu(gpu='gpu', cpu='cpu', type_=str(i), num='size')
            cu_gen.close_brace(0)
            cu_gen.blank_line()

            cu_gen.line_no_end(
                "void gpu2cpu_{}({} *gpu, {} *cpu, int size)".format(i, i, i),
                0)
            cu_gen.open_brace(0)
            cu_gen.gpu_to_cpu(gpu='gpu', cpu='cpu', type_=str(i), num='size')
            cu_gen.close_brace(0)
            cu_gen.blank_line()

            cu_gen.line_no_end(
                "{}* to_gpu_{}({} *cpu, int size)".format(i, i, i), 0)
            cu_gen.open_brace(0)
            cu_gen.line(line='{} * gpu = NULL'.format(i))
            cu_gen.to_gpu(ret='gpu', cpu='cpu', type_=str(i), num='size')
            cu_gen.line(line='return gpu', tab=1)
            cu_gen.close_brace(0)
            cu_gen.blank_line()

            cu_gen.line_no_end(
                "{}* from_gpu_{}({} *gpu, int size)".format(i, i, i), 0)
            cu_gen.open_brace(0)
            cu_gen.from_gpu(gpu='gpu', ret='cpu', type_=str(i), num='size')
            cu_gen.line(line='return cpu', tab=1)
            cu_gen.close_brace(0)
            cu_gen.blank_line()

        cu_gen.blank_line()
        cu_gen.close()

        return
Esempio n. 2
0
    def _generate_data_cu(self):
        cu_gen = CUDAGenerator(
            '{}/code_gen/connection.data.cu'.format(pkg_dir))

        cu_gen.blank_line(2)
        if self.debug:
            cu_gen.include_std('stdio.h')

        cu_gen.include_std('stdlib.h')
        cu_gen.blank_line()
        cu_gen.include('../c_code/helper_cuda.h')
        cu_gen.include('connection.h')
        cu_gen.blank_line(2)

        cu_gen.line_no_end("CConnection * to_gpu_connection(CConnection *cpu)",
                           0)
        cu_gen.open_brace()
        cu_gen.line(
            'CConnection * gpu = (CConnection*)malloc(sizeof(CConnection))')
        cu_gen.line('gpu->n_len = cpu->n_len')
        cu_gen.line('gpu->r_n_len = cpu->r_n_len')
        cu_gen.line('gpu->s_len = cpu->s_len')

        cu_gen.to_gpu(ret='gpu->delay_start',
                      cpu='cpu->delay_start',
                      type_='int',
                      num='cpu->n_len')
        cu_gen.to_gpu(ret='gpu->delay_num',
                      cpu='cpu->delay_num',
                      type_='int',
                      num='cpu->n_len')
        cu_gen.to_gpu(ret='gpu->rev_delay_start',
                      cpu='cpu->rev_delay_start',
                      type_='int',
                      num='cpu->r_n_len')
        cu_gen.to_gpu(ret='gpu->rev_delay_num',
                      cpu='cpu->rev_delay_num',
                      type_='int',
                      num='cpu->r_n_len')
        cu_gen.to_gpu(ret='gpu->rev_map2sid',
                      cpu='cpu->rev_map2sid',
                      type_='int',
                      num='cpu->s_len')

        cu_gen.line('CConnection * ret = NULL')
        cu_gen.to_gpu(ret='ret', cpu='gpu', type_='CConnection')

        if self.debug:
            cu_gen.line(line=r'printf("GPU CConnection Pointer: %p\n", ret)')
            cu_gen.line(
                line=
                r'printf("GPU n_len: %d r_n_len: %d s_len: %d\n", gpu->n_len, gpu->r_n_len, gpu->s_len)'
            )

        cu_gen.line('return ret')
        cu_gen.close_brace()
        cu_gen.blank_line()

        cu_gen.line_no_end(
            "CConnection * from_gpu_connection(CConnection *gpu)", 0)
        cu_gen.open_brace()
        cu_gen.from_gpu(gpu='gpu', ret='ret', type_='CConnection')

        if self.debug:
            cu_gen.line(line=r'printf("CPU CConnection Pointer: %p\n", ret)')
            cu_gen.line(
                line=
                r'printf("CPU n_len: %d r_n_len: %d s_len: %d\n", ret->n_len, ret->r_n_len, ret->s_len)'
            )

        cu_gen.line('return ret')
        cu_gen.close_brace()
        cu_gen.blank_line()

        cu_gen.close()
        return
Esempio n. 3
0
    def generate_data_cu(self, debug=False):
        cu_gen = CUDAGenerator('{}/code_gen/{}.data.cu'.format(
            pkg_dir, self.name.lower()))

        cu_gen.blank_line(2)
        cu_gen.include_std('stdlib.h')
        cu_gen.blank_line()
        cu_gen.include("../c_code/helper_cuda.h")
        cu_gen.include("{}.h".format(self.name.lower()))
        cu_gen.blank_line(2)

        cu_gen.line_no_end(
            "{} * to_gpu_{}({} *cpu, int num)".format(self.name.capitalize(),
                                                      self.name.lower(),
                                                      self.name.capitalize()),
            0)
        cu_gen.open_brace()
        cu_gen.malloc(ret='gpu', type_=self.name.capitalize())

        for i in self.parameters['special']:
            if i != 'delay':
                cu_gen.to_gpu(ret='gpu->p_{}'.format(i),
                              cpu='cpu->p_{}'.format(i),
                              num='num',
                              type_='int')

        for i in self.parameters['constant']:
            cu_gen.to_gpu(ret='gpu->p_{}'.format(i),
                          cpu='cpu->p_{}'.format(i),
                          num='num',
                          type_=real)

        for i in self.parameters['variable']:
            cu_gen.to_gpu(ret='gpu->p_{}'.format(i),
                          cpu='cpu->p_{}'.format(i),
                          num='num',
                          type_=real)

        cu_gen.line('{} * ret = NULL'.format(self.name.capitalize()))
        cu_gen.to_gpu(ret='ret',
                      cpu='gpu',
                      num='1',
                      type_=self.name.capitalize())

        cu_gen.line('return ret')
        cu_gen.close_brace()
        cu_gen.blank_line()

        cu_gen.line_no_end(
            "{} * from_gpu_{}({} *gpu, int num)".format(
                self.name.capitalize(), self.name.lower(),
                self.name.capitalize()), 0)
        cu_gen.open_brace()
        cu_gen.from_gpu(ret='cpu', gpu='gpu', type_=self.name.capitalize())

        cu_gen.line('return cpu')
        cu_gen.close_brace()
        cu_gen.blank_line()

        cu_gen.close()
        return