Beispiel #1
0
 def provide_variables(text):
     result = []
     for vars, line in text:
         result.append(([], line))
         result += map(
             lambda x: ([], Simple_line(line.i, x, line.filename)),
             call_provides(vars))
     return result
Beispiel #2
0
 def builder(self):
   if '_builder' not in self.__dict__:
     if not self.is_main:
       self._builder = []
     else:
       import parsed_text
       from variables import build_use, call_provides
       for filename,buffer in parsed_text.parsed_text:
         if self.line.filename[0].startswith(filename):
           break
       text = []
       same_as = self.same_as
       inside = False
       for vars,line in buffer:
         if type(line) == Begin_provider:
           if line.filename[1] == same_as:
             inside = True
           vars = []
         if inside:
           text.append( (vars,line) )
           text += map( lambda x: ([],Simple_line(line.i,x,line.filename)), call_provides(vars) )
           if command_line.do_profile and type(line) == Begin_provider:
             text.append( ( [], Declaration(line.i,"  double precision :: irp_rdtsc, irp_rdtsc1, irp_rdtsc2",line.filename) ) )
             text.append( ( [], Simple_line(line.i,"  irp_rdtsc1 = irp_rdtsc()",line.filename) ) )
         if type(line) == End_provider:
           if inside:
             break
       name = self.name
       text = parsed_text.move_to_top(text,Declaration)
       text = parsed_text.move_to_top(text,Implicit)
       text = parsed_text.move_to_top(text,Use)
       text = map(lambda x: x[1], text)
       for line in filter(lambda x: type(x) not in [ Begin_doc, End_doc, Doc], text):
         if type(line) == Begin_provider:
           result = []
           if command_line.directives and command_line.inline in ["all","builders"]:
             result += [ "!DEC$ ATTRIBUTES INLINE :: bld_%s"%(same_as) ]
           result += [ "subroutine bld_%s"%(name) ]
           result += build_use([name]+self.needs)
         elif type(line) == Cont_provider:
           pass
         elif type(line) == End_provider:
           if command_line.do_profile:
             result += [ "  irp_rdtsc2 = irp_rdtsc()" ,
                         "  call irp_set_timer(%d,(irp_rdtsc2-irp_rdtsc1))"%self.label ]
           result.append( "end subroutine bld_%s"%(name) )
           break
         else:
           result.append(line.text)
       self._builder = result
   return self._builder
Beispiel #3
0
  def provider(self):
    if '_provider' not in self.__dict__:
     if not self.is_main:
       self._provider = []
     else:
      if '_to_provide' not in self.__dict__:
        import parsed_text
      from variables import variables, build_use, call_provides
      name = self.name
      same_as = self.same_as

      def build_alloc(name):
        self = variables[name]
        if self.dim == []:
           return []

        def do_size():
           result = "     print *, ' size: ("
           result += ','.join(self.dim)
           return result+")'"

        def check_dimensions():
          result = map(lambda x: "(%s>0)"%(dimsize(x)), self.dim)
          result = ".and.".join(result)
          result = "   if (%s) then"%(result)
          return result
 
        def dimensions_OK():
          result = [ "  irp_dimensions_OK = .True." ]
          for i,k in enumerate(self.dim):
              result.append("  irp_dimensions_OK = irp_dimensions_OK.AND.(SIZE(%s,%d)==(%s))"%(name,i+1,dimsize(k)))
          return result

        def do_allocate():
          result = "    allocate(%s(%s),stat=irp_err)"
          result = result%(name,','.join(self.dim))
          if command_line.do_memory:
            tmp = "\n   print *, %s, 'Allocating %s(%s)'"
            d = ','.join(self.dim)
            result += tmp%('size('+name+')',name,d)
          return result

        result = [ " if (allocated (%s) ) then"%(name) ]
        result += dimensions_OK()
        result += [\
          "  if (.not.irp_dimensions_OK) then",
          "   deallocate(%s,stat=irp_err)"%(name),
          "   if (irp_err /= 0) then",
          "     print *, irp_here//': Deallocation failed: %s'"%(name),
          do_size(),
          "   endif"]
        if command_line.do_memory:
          result += [\
          "   print *, 'Deallocating %s'"%(name) ]
        result.append(check_dimensions())
        result.append(do_allocate())
        result += [\
          "    if (irp_err /= 0) then",
          "     print *, irp_here//': Allocation failed: %s'"%(name),
          do_size(),
          "    endif",
          "   endif",
          "  endif",
          " else" ]
        result.append(check_dimensions())
        result.append(do_allocate())
        result += [\
          "    if (irp_err /= 0) then",
          "     print *, irp_here//': Allocation failed: %s'"%(name),
          do_size(),
          "    endif",
          "   endif",
          " endif" ]
        return result

      result = []
      if command_line.directives and command_line.inline in ["all","providers"]:
        result += [ "!DEC$ ATTRIBUTES FORCEINLINE :: provide_%s"%(name) ]
      result += [ "subroutine provide_%s"%(name) ] 
      result += build_use( [same_as]+self.to_provide )
      result.append("  implicit none")
      length = len("provide_%s"%(name))
      result += [\
      "  character*(%d) :: irp_here = 'provide_%s'"%(length,name),
      "  integer                   :: irp_err ",
      "  logical                   :: irp_dimensions_OK" ]
      if command_line.do_openmp:
        result.append(" call irp_lock_%s(.True.)"%(same_as))
      if command_line.do_assert or command_line.do_debug:
        result.append("  call irp_enter(irp_here)")
      result += call_provides(self.to_provide)
      result += flatten( map(build_alloc,[self.same_as]+self.others) )
      result += [ " if (.not.%s_is_built) then"%(same_as),
                  "  call bld_%s"%(same_as),
                  "  %s_is_built = .True."%(same_as), "" ]
      result += [ " endif" ]
      if command_line.do_assert or command_line.do_debug:
        result.append("  call irp_leave(irp_here)")
      if command_line.do_openmp:
        result.append(" call irp_lock_%s(.False.)"%(same_as))
      result.append("end subroutine provide_%s"%(name) )
      result.append("")
      self._provider = result
    return self._provider
Beispiel #4
0
 def provide_variables(text):
   result = []
   for vars,line in text:
     result.append( ([],line) )
     result += map(lambda x: ([],Simple_line(line.i,x,line.filename)), call_provides(vars)) 
   return result
Beispiel #5
0
    def builder(self):
        if '_builder' not in self.__dict__:
            if not self.is_main:
                self._builder = []
            else:
                import parsed_text
                from variables import build_use, call_provides
                for filename, buffer in parsed_text.parsed_text:
                    if self.line.filename[0].startswith(filename):
                        break
                text = []
                same_as = self.same_as
                inside = False
                for vars, line in buffer:
                    if type(line) == Begin_provider:
                        if line.filename[1] == same_as:
                            inside = True
                        vars = []
                    if inside:
                        text.append((vars, line))
                        text += map(
                            lambda x:
                            ([], Simple_line(line.i, x, line.filename)),
                            call_provides(vars))
                        if command_line.do_profile and type(
                                line) == Begin_provider:
                            text.append((
                                [],
                                Declaration(
                                    line.i,
                                    "  double precision :: irp_rdtsc, irp_rdtsc1, irp_rdtsc2",
                                    line.filename)))
                            text.append(
                                ([],
                                 Simple_line(line.i,
                                             "  irp_rdtsc1 = irp_rdtsc()",
                                             line.filename)))
                    if type(line) == End_provider:
                        if inside:
                            break
                name = self.name
                text = parsed_text.move_to_top(text, Declaration)
                text = parsed_text.move_to_top(text, Implicit)
                text = parsed_text.move_to_top(text, Use)
                text = map(lambda x: x[1], text)
                #        inside_omp = False
                for line in filter(
                        lambda x: type(x) not in [Begin_doc, End_doc, Doc],
                        text):
                    if type(line) == Begin_provider:
                        result = []
                        if command_line.directives and command_line.inline in [
                                "all", "builders"
                        ]:
                            result += [
                                "!DEC$ ATTRIBUTES INLINE :: bld_%s" % (same_as)
                            ]
                        result += ["subroutine bld_%s" % (name)]
                        result += build_use([name] + self.needs)
                    elif type(line) == Cont_provider:
                        pass
                    elif type(line) == End_provider:
                        if command_line.do_profile:
                            result += [
                                "  irp_rdtsc2 = irp_rdtsc()",
                                "  call irp_set_timer(%d,(irp_rdtsc2-irp_rdtsc1))"
                                % self.label
                            ]
                        result.append("end subroutine bld_%s" % (name))
                        break
                    elif type(line) == Openmp:
                        # Detect OpenMP blocks
                        buffer = line.text.lower().split()
                        if buffer[1] == "parallel":
                            #              inside_omp = True
                            self._has_openmp = True
#            if buffer[1] == "end" and buffer[2] == "parallel":
#              inside_omp = False
                        result.append(line.text)
                    else:
                        #           if inside_omp:
                        #             if type(line) in [ Provide_all, Provide, Touch, SoftTouch ]:
                        #               error.fail(line,str(type(line))+"is not allowed in an OpenMP block.")
                        result.append(line.text)
                self._builder = result
        return self._builder
Beispiel #6
0
    def provider(self):
        if '_provider' not in self.__dict__:
            if not self.is_main:
                self._provider = []
            else:
                if '_to_provide' not in self.__dict__:
                    import parsed_text
                from variables import variables, build_use, call_provides
                name = self.name
                same_as = self.same_as

                def build_alloc(name):
                    self = variables[name]
                    if self.dim == []:
                        return []

                    def do_size():
                        result = "     print *, ' size: ("
                        result += ','.join(self.dim)
                        return result + ")'"

                    def check_dimensions():
                        result = map(lambda x: "(%s>0)" % (dimsize(x)),
                                     self.dim)
                        result = ".and.".join(result)
                        result = "   if (%s) then" % (result)
                        return result

                    def dimensions_OK():
                        result = ["  irp_dimensions_OK = .True."]
                        for i, k in enumerate(self.dim):
                            result.append(
                                "  irp_dimensions_OK = irp_dimensions_OK.AND.(SIZE(%s,%d)==(%s))"
                                % (name, i + 1, dimsize(k)))
                        return result

                    def do_allocate():
                        if command_line.coarray:
                            result = "    allocate(%s(%s)[*],stat=irp_err)"
                        else:
                            result = "    allocate(%s(%s),stat=irp_err)"
                        result = result % (name, ','.join(self.dim))
                        if command_line.do_memory:
                            tmp = "\n   print *, %s, 'Allocating %s(%s)'"
                            d = ','.join(self.dim)
                            result += tmp % ('size(' + name + ')', name, d)
                        return result

                    result = [" if (allocated (%s) ) then" % (name)]
                    result += dimensions_OK()
                    result += [\
                      "  if (.not.irp_dimensions_OK) then",
                      "   deallocate(%s,stat=irp_err)"%(name),
                      "   if (irp_err /= 0) then",
                      "     print *, irp_here//': Deallocation failed: %s'"%(name),
                      do_size(),
                      "   endif"]
                    if command_line.do_memory:
                        result += [\
                        "   print *, 'Deallocating %s'"%(name) ]
                    result.append(check_dimensions())
                    result.append(do_allocate())
                    result += [\
                      "    if (irp_err /= 0) then",
                      "     print *, irp_here//': Allocation failed: %s'"%(name),
                      do_size(),
                      "    endif",
                      "   endif",
                      "  endif",
                      " else" ]
                    result.append(check_dimensions())
                    result.append(do_allocate())
                    result += [\
                      "    if (irp_err /= 0) then",
                      "     print *, irp_here//': Allocation failed: %s'"%(name),
                      do_size(),
                      "    endif",
                      "   endif",
                      " endif" ]
                    return result

                result = []
                if command_line.directives and command_line.inline in [
                        "all", "providers"
                ]:
                    result += [
                        "!DEC$ ATTRIBUTES FORCEINLINE :: provide_%s" % (name)
                    ]
                result += ["subroutine provide_%s" % (name)]
                result += build_use([same_as] + self.to_provide)
                if command_line.do_openmp:
                    result += [" use omp_lib"]
                result.append("  implicit none")
                length = len("provide_%s" % (name))
                result += [\
                "  character*(%d) :: irp_here = 'provide_%s'"%(length,name),
                "  integer                   :: irp_err ",
                "  logical                   :: irp_dimensions_OK",
                "!$ integer                  :: nthreads"]
                if command_line.do_openmp:
                    result.append(" call irp_lock_%s(.True.)" % (same_as))
                if command_line.do_debug:
                    result.append("  call irp_enter(irp_here)")
                result += call_provides(self.to_provide)
                result += flatten(
                    map(build_alloc, [self.same_as] + self.others))
                result += [
                    " if (.not.%s_is_built) then" % (same_as),
                    "  call bld_%s" % (same_as),
                    "  %s_is_built = .True." % (same_as), ""
                ]
                result += [" endif"]
                if command_line.do_debug:
                    result.append("  call irp_leave(irp_here)")
                if command_line.do_openmp:
                    result.append(" call irp_lock_%s(.False.)" % (same_as))
                result.append("end subroutine provide_%s" % (name))
                result.append("")
                self._provider = result
        return self._provider