Karl Rupp avatar Karl Rupp committed ad0a213 Merge

Merge branch 'karlrupp/cmake-cuda-builds'

Comments (0)

Files changed (3)

config/PETSc/Configure.py

       except (ImportError, KeyError), e:
         self.framework.logPrint('Importing cmakeboot failed:\n' + str(e))
       if self.cmakeboot_success:
-        if self.framework.argDB['with-cuda']: # Our CMake build does not support CUDA at this time
-          self.framework.logPrint('CMake configured successfully, but could not be used by default because --with-cuda was used\n')
-        elif hasattr(self.compilers, 'FC') and self.compilers.fortranIsF90 and not self.setCompilers.fortranModuleOutputFlag:
+        if hasattr(self.compilers, 'FC') and self.compilers.fortranIsF90 and not self.setCompilers.fortranModuleOutputFlag:
           self.framework.logPrint('CMake configured successfully, but could not be used by default because of missing fortranModuleOutputFlag\n')
         else:
           self.framework.logPrint('CMake configured successfully, using as default build\n')

config/cmakeboot.py

    langlist = [('C','C')]
    if hasattr(self.compilers,'FC'):
      langlist.append(('FC','Fortran'))
+   if hasattr(self.compilers,'CUDAC'):
+     langlist.append(('CUDA','CUDA'))
    if (self.languages.clanguage == 'Cxx'):
      langlist.append(('Cxx','CXX'))
    win32fe = None
    for petsclanguage,cmakelanguage in langlist:
      self.setCompilers.pushLanguage(petsclanguage)
      compiler = self.setCompilers.getCompiler()
-     flags = [self.setCompilers.getCompilerFlags(),
-              self.setCompilers.CPPFLAGS,
-              self.CHUD.CPPFLAGS]
-     if compiler.split()[0].endswith('win32fe'): # Hack to support win32fe without changing the rest of configure
-       win32fe = compiler.split()[0] + '.exe'
-       compiler = ' '.join(compiler.split()[1:])
-     options.append('-DCMAKE_'+cmakelanguage+'_COMPILER:FILEPATH=' + compiler)
-     options.append('-DCMAKE_'+cmakelanguage+'_FLAGS:STRING=' + ''.join(flags))
-     self.setCompilers.popLanguage()
-   options.append('-DCMAKE_AR='+self.setCompilers.AR)
+     if (cmakelanguage == 'CUDA'):
+       self.cuda = self.framework.require('PETSc.packages.cuda',       None)
+       if (self.cuda.directory != None):
+         options.append('CUDA_TOOLKIT_ROOT_DIR ' + self.cuda.directory + ' CACHE FILEPATH')
+       options.append('CUDA_NVCC_FLAGS ' + self.setCompilers.getCompilerFlags() + ' CACHE STRING')
+     else:
+       flags = [self.setCompilers.getCompilerFlags(),
+                self.setCompilers.CPPFLAGS,
+                self.CHUD.CPPFLAGS]
+       if compiler.split()[0].endswith('win32fe'): # Hack to support win32fe without changing the rest of configure
+         win32fe = compiler.split()[0] + '.exe'
+         compiler = ' '.join(compiler.split()[1:])
+       options.append('CMAKE_'+cmakelanguage+'_COMPILER ' + compiler + ' CACHE FILEPATH')
+       options.append('CMAKE_'+cmakelanguage+'_FLAGS "' + ''.join(flags) + '" CACHE STRING')
+       if (petsclanguage == self.languages.clanguage): #CUDA host compiler is fed with the flags for the standard host compiler
+         flagstring = ''
+         for flag in flags:
+           for f in flag.split():
+             flagstring += ',' + f
+         options.append('PETSC_CUDA_HOST_FLAGS ' + flagstring + ' CACHE STRING')
+       self.setCompilers.popLanguage()
+   options.append('CMAKE_AR '+self.setCompilers.AR + " CACHE FILEPATH")
    ranlib = shlex.split(self.setCompilers.RANLIB)[0]
-   options.append('-DCMAKE_RANLIB='+ranlib)
+   options.append('CMAKE_RANLIB '+ranlib + " CACHE FILEPATH")
+   if win32fe:
+     options.append('PETSC_WIN32FE %s' % win32fe)
+     
+   archdir = os.path.join(self.petscdir.dir, self.arch.arch)
+   initial_cache_filename = os.path.join(archdir, 'initial_cache_file.cmake')  
+   cmd = [self.cmake.cmake, '--trace', '--debug-output', '-C' + str(initial_cache_filename), '-DPETSC_CMAKE_ARCH:STRING='+str(self.arch.arch), self.petscdir.dir] + args
    if win32fe:
-     options.append('-DPETSC_WIN32FE:FILEPATH=%s'%win32fe)
      # Default on Windows is to generate Visual Studio project files, but
      # 1. the build process for those is different, need to give different build instructions
      # 2. the current WIN32FE workaround does not work with VS project files
-     options.append('-GUnix Makefiles')
-   cmd = [self.cmake.cmake, '--trace', '--debug-output', self.petscdir.dir] + map(lambda x:x.strip(), options) + args
-   archdir = os.path.join(self.petscdir.dir, self.arch.arch)
+     cmd.append('-GUnix Makefiles')
+
+   # Create inital cache file:
+   initial_cache_file = open(initial_cache_filename, 'w')
+   self.logPrint('Contents of initial cache file %s :' % initial_cache_filename)
+   for option in options:
+     initial_cache_file.write('SET (' + option + ' "Dummy comment" FORCE)\n')
+     self.logPrint('SET (' + option + ' "Dummy comment" FORCE)\n')
+   initial_cache_file.close()   
    try:
      # Try to remove the old cache because some versions of CMake lose CMAKE_C_FLAGS when reconfiguring this way
+     self.logPrint('Removing: %s' % os.path.join(archdir, 'CMakeCache.txt'))
      os.remove(os.path.join(archdir, 'CMakeCache.txt'))
    except OSError:
      pass
    self.logPrint('Removing: %s' % os.path.join(archdir, 'CMakeFiles', version.vstring))
    shutil.rmtree(os.path.join(archdir, 'CMakeFiles', version.vstring), ignore_errors=True)
    log.write('Invoking: %s\n' % cmd)
-   output,error,retcode = self.executeShellCommand(cmd, checkCommand = noCheck, log=log, cwd=archdir,timeout=30)
+   output,error,retcode = self.executeShellCommand(cmd, checkCommand = noCheck, log=log, cwd=archdir,timeout=300)
    if retcode:
      self.logPrintBox('CMake setup incomplete (status %d), falling back to legacy build' % (retcode,))
      self.logPrint('Output: '+output+'\nError: '+error)

config/cmakegen.py

                           'symmetric diff',sorted(smdirs.symmetric_difference(sdirs))))
   def compareSourceLists(msources, files):
     smsources = set(msources)
-    ssources  = set(f for f in files if os.path.splitext(f)[1] in ['.c', '.cxx', '.cc', '.cpp', '.F'])
+    ssources  = set(f for f in files if os.path.splitext(f)[1] in ['.c', '.cxx', '.cc', '.cu', '.cpp', '.F'])
     if not smsources.issubset(ssources):
       MISTAKES.append('Makefile contains file not on filesystem: %s: %r' % (root, sorted(smsources - ssources)))
     if not VERBOSE: return
       conditions.update(set(tuple(stripsplit(line)) for line in lines if line.startswith('#requires')))
     def relpath(filename):
       return os.path.join(root,filename)
+    sourcecu = makevars.get('SOURCECU','').split()
     sourcec = makevars.get('SOURCEC','').split()
     sourcef = makevars.get('SOURCEF','').split()
-    compareSourceLists(sourcec+sourcef, files) # Diagnostic output about unused source files
-    sources[repr(sorted(conditions))].extend(relpath(f) for f in sourcec + sourcef)
+    compareSourceLists(sourcec+sourcef+sourcecu, files) # Diagnostic output about unused source files
+    sources[repr(sorted(conditions))].extend(relpath(f) for f in sourcec + sourcef + sourcecu)
     allconditions[root] = conditions
   return sources
 
   f.write(r'''cmake_minimum_required (VERSION 2.6.2)
 project (PETSc C)
 
-include (${PETSc_BINARY_DIR}/conf/PETScConfig.cmake)
+include (${PETSC_CMAKE_ARCH}/conf/PETScConfig.cmake)
 
 if (PETSC_HAVE_FORTRAN)
   enable_language (Fortran)
   SET(CMAKE_Fortran_ARCHIVE_FINISH "<CMAKE_RANLIB> -c <TARGET> ")
 endif ()
 
+if (PETSC_HAVE_CUDA)
+  find_package (CUDA REQUIRED)
+  set (CUDA_PROPAGATE_HOST_FLAGS OFF)
+  set (CUDA_NVCC_FLAGS ${CUDA_NVCC_FLAGS} --compiler-options ${PETSC_CUDA_HOST_FLAGS})
+endif ()
+
 include_directories ("${PETSc_SOURCE_DIR}/include" "${PETSc_BINARY_DIR}/include")
 
 add_definitions (-D__INSDIR__= ) # CMake always uses the absolute path
       f.write(body(0))
   f.write('''
 if (NOT PETSC_USE_SINGLE_LIBRARY)
-  add_library (petsc%(pkg)s ${PETSC%(PKG)s_SRCS})
+  if (PETSC_HAVE_CUDA)
+    cuda_add_library (petsc%(pkg)s ${PETSC%(PKG)s_SRCS})
+  else ()
+    add_library (petsc%(pkg)s ${PETSC%(PKG)s_SRCS})
+  endif ()
   target_link_libraries (petsc%(pkg)s %(pkgdeps)s ${PETSC_PACKAGE_LIBS})
   if (PETSC_WIN32FE)
     set_target_properties (petsc%(pkg)s PROPERTIES RULE_LAUNCH_COMPILE "${PETSC_WIN32FE}")
         writePackage(f,pkg,deps.split())
       f.write ('''
 if (PETSC_USE_SINGLE_LIBRARY)
-  add_library (petsc %s)
+  if (PETSC_HAVE_CUDA)
+    cuda_add_library (petsc %(allsrc)s)
+  else ()
+    add_library (petsc %(allsrc)s)
+  endif ()
   target_link_libraries (petsc ${PETSC_PACKAGE_LIBS})
   if (PETSC_WIN32FE)
     set_target_properties (petsc PROPERTIES RULE_LAUNCH_COMPILE "${PETSC_WIN32FE}")
   endif ()
 
 endif ()
-''' % (' '.join([r'${PETSC' + pkg.upper() + r'_SRCS}' for pkg,deps in pkglist]),))
+''' % dict(allsrc=' '.join([r'${PETSC' + pkg.upper() + r'_SRCS}' for pkg,deps in pkglist])))
       f.write('''
 if (PETSC_CLANGUAGE_Cxx)
   foreach (file IN LISTS %s)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.