Commits

Karl Rupp committed 65e3cb3

Adjusted parsing of ViennaCL device type options.

The 'old' model required the creation of a vector before creating a matrix (bogus).
This is now resolved and options are considered irrespective of the vector/matrix creation order.

  • Participants
  • Parent commits 5e5cd3c

Comments (0)

Files changed (6)

src/mat/impls/aij/mpi/mpiviennacl/mpiaijviennacl.cxx

 #include "petscconf.h"
 #include <../src/mat/impls/aij/mpi/mpiaij.h>   /*I "petscmat.h" I*/
+#include <../src/mat/impls/aij/seq/seqviennacl/viennaclmatimpl.h>
 
 #undef __FUNCT__
 #define __FUNCT__ "MatMPIAIJSetPreallocation_MPIAIJViennaCL"
   ierr = PetscObjectComposeFunction((PetscObject)A,"MatMPIAIJSetPreallocation_C",MatMPIAIJSetPreallocation_MPIAIJViennaCL);CHKERRQ(ierr);
   A->ops->getvecs        = MatGetVecs_MPIAIJViennaCL;
 
+  ierr = MatSetFromOptions_SeqViennaCL(A);CHKERRQ(ierr); /* Allows to set device type before allocating any objects */
   ierr = PetscObjectChangeTypeName((PetscObject)A,MATMPIAIJVIENNACL);CHKERRQ(ierr);
   PetscFunctionReturn(0);
 }

src/mat/impls/aij/seq/seqviennacl/aijviennacl.cxx

   B->ops->destroy        = MatDestroy_SeqAIJViennaCL;
   B->ops->getvecs        = MatGetVecs_SeqAIJViennaCL;
 
+  ierr = MatSetFromOptions_SeqViennaCL(B);CHKERRQ(ierr); /* Allows to set device type before allocating any objects */
   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJVIENNACL);CHKERRQ(ierr);
 
   B->valid_GPU_matrix = PETSC_VIENNACL_UNALLOCATED;

src/mat/impls/aij/seq/seqviennacl/viennaclmatimpl.h

   ViennaCLAIJMatrix      *mat;  /* pointer to the matrix on the GPU */
 };
 
+#undef __FUNCT__
+#define __FUNCT__ "MatSetFromOptions_SeqViennaCL"
+static PetscErrorCode MatSetFromOptions_SeqViennaCL(Mat A)
+{
+  PetscErrorCode       ierr;
+  PetscBool            flg;
+
+  PetscFunctionBegin;
+  ViennaCLSetFromOptions((PetscObject)A);
+  PetscFunctionReturn(0);
+}
 
 PETSC_INTERN PetscErrorCode MatViennaCLCopyToGPU(Mat);
 PETSC_INTERN PetscErrorCode MatViennaCLCopyFromGPU(Mat, ViennaCLAIJMatrix*);

src/vec/vec/impls/mpi/mpiviennacl/mpiviennacl.cxx

      reset array?
      get values?
   */
+  ierr = VecSetFromOptions_SeqViennaCL(vv);CHKERRQ(ierr); /* Allows to set device type before allocating any objects */
   ierr = VecViennaCLAllocateCheck(vv);CHKERRQ(ierr);
   vv->valid_GPU_array      = PETSC_VIENNACL_GPU;
   ierr = VecSet(vv,0.0);CHKERRQ(ierr);

src/vec/vec/impls/seq/seqviennacl/vecviennacl.cxx

   PetscFunctionReturn(0);
 }
 
-#undef __FUNCT__
-#define __FUNCT__ "VecSetFromOptions_SeqViennaCL"
-PetscErrorCode VecSetFromOptions_SeqViennaCL(Vec v)
-{
-  PetscErrorCode       ierr;
-  PetscBool            flg;
-
-  PetscFunctionBegin;
-  ierr = PetscOptionsHead("SeqAIJCUSP options");CHKERRQ(ierr);
-  ierr = PetscObjectOptionsBegin((PetscObject)v);
-
-  ierr = PetscOptionsHasName(NULL,"-viennacl_device_cpu",&flg);CHKERRQ(ierr);
-  if (flg) {
-    try {
-      viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_CPU);
-    } catch (std::exception const & ex) {
-      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
-    }
-  }
-  ierr = PetscOptionsHasName(NULL,"-viennacl_device_gpu",&flg);CHKERRQ(ierr);
-  if (flg) {
-    try {
-      viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_GPU);
-    } catch (std::exception const & ex) {
-      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
-    }
-  }
-  ierr = PetscOptionsHasName(NULL,"-viennacl_device_accelerator",&flg);CHKERRQ(ierr);
-  if (flg) {
-    try {
-      viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_ACCELERATOR);
-    } catch (std::exception const & ex) {
-      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
-    }
-  }
-
-  ierr = PetscOptionsEnd();CHKERRQ(ierr);
-  PetscFunctionReturn(0);
-
-}
-
 
 #undef __FUNCT__
 #define __FUNCT__ "VecCreate_SeqViennaCL"

src/vec/vec/impls/seq/seqviennacl/viennaclvecimpl.h

 
 
 #undef __FUNCT__
+#define __FUNCT__ "ViennaCLSetFromOptions"
+static PetscErrorCode ViennaCLSetFromOptions(PetscObject obj)
  1. Jed Brown

    Karl Rupp Why did you move this to the header? Since it's static, we have -Wunused-function warnings, but why not just make it extern and leave it in an implementation file?

    And FWIW, I'd rather call it PetscObjectSetFromOptions_ViennaCL.

    1. Karl Rupp author

      I wanted to reuse it from src/mat/. Making it extern is an option, but this way it is visible in the ABI, which is not great either.

      1. Jed Brown

        We should have something between PETSC_INTERN and PETSC_EXTERN that is only available within PETSc (i.e., becomes PETSC_INTERN for the default single-library, but is PETSC_EXTERN for separate libraries).

        We have other data members that are visible externally, but definitely not part of the public API (due to internal sharing). As long as users don't call those functions, libpetsc*.so can be upgraded without breaking binaries. I would rather compile that bit of code only once, warning free, than have it compiled several times to avoid exposing an undocumented symbol in the binary.

+{
+  PetscErrorCode       ierr;
+  PetscBool            flg;
+
+  PetscFunctionBegin;
+  ierr = PetscObjectOptionsBegin(obj);
+
+  ierr = PetscOptionsHasName(NULL,"-viennacl_device_cpu",&flg);CHKERRQ(ierr);
+  if (flg) {
+    try {
+      viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_CPU);
+    } catch (std::exception const & ex) {
+      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
+    }
+  }
+  ierr = PetscOptionsHasName(NULL,"-viennacl_device_gpu",&flg);CHKERRQ(ierr);
+  if (flg) {
+    try {
+      viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_GPU);
+    } catch (std::exception const & ex) {
+      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
+    }
+  }
+  ierr = PetscOptionsHasName(NULL,"-viennacl_device_accelerator",&flg);CHKERRQ(ierr);
+  if (flg) {
+    try {
+      viennacl::ocl::set_context_device_type(0, CL_DEVICE_TYPE_ACCELERATOR);
+    } catch (std::exception const & ex) {
+      SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"ViennaCL error: %s", ex.what());
+    }
+  }
+
+  ierr = PetscOptionsEnd();CHKERRQ(ierr);
+  PetscFunctionReturn(0);
+}
+
+
+#undef __FUNCT__
+#define __FUNCT__ "VecSetFromOptions_SeqViennaCL"
+static PetscErrorCode VecSetFromOptions_SeqViennaCL(Vec v)
+{
+  PetscErrorCode       ierr;
+  PetscBool            flg;
+
+  PetscFunctionBegin;
+  ViennaCLSetFromOptions((PetscObject)v);
+  PetscFunctionReturn(0);
+}
+
+#undef __FUNCT__
 #define __FUNCT__ "VecViennaCLGetArrayReadWrite"
 PETSC_STATIC_INLINE PetscErrorCode VecViennaCLGetArrayReadWrite(Vec v, ViennaCLVector **a)
 {