1e5c89e4eSSatish Balay 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay We define the memory operations here. The reason we just do not use 4e5c89e4eSSatish Balay the standard memory routines in the PETSc code is that on some machines 5e5c89e4eSSatish Balay they are broken. 6e5c89e4eSSatish Balay 7e5c89e4eSSatish Balay */ 8c6db04a5SJed Brown #include <petscsys.h> /*I "petscsys.h" I*/ 9c6db04a5SJed Brown #include <petscbt.h> 10c6db04a5SJed Brown #include <../src/sys/utils/ftn-kernels/fcopy.h> 118a2c3ff8SSatish Balay #if defined(PETSC_HAVE_STRING_H) 128a2c3ff8SSatish Balay #include <string.h> 138a2c3ff8SSatish Balay #endif 14e5c89e4eSSatish Balay 154610e317SSatish Balay /*@ 16e5c89e4eSSatish Balay PetscMemcmp - Compares two byte streams in memory. 17e5c89e4eSSatish Balay 18e5c89e4eSSatish Balay Not Collective 19e5c89e4eSSatish Balay 20e5c89e4eSSatish Balay Input Parameters: 21e5c89e4eSSatish Balay + str1 - Pointer to the first byte stream 22e5c89e4eSSatish Balay . str2 - Pointer to the second byte stream 23e5c89e4eSSatish Balay - len - The length of the byte stream 24e5c89e4eSSatish Balay (both str1 and str2 are assumed to be of length len) 25e5c89e4eSSatish Balay 26e5c89e4eSSatish Balay Output Parameters: 27e5c89e4eSSatish Balay . e - PETSC_TRUE if equal else PETSC_FALSE. 28e5c89e4eSSatish Balay 29e5c89e4eSSatish Balay Level: intermediate 30e5c89e4eSSatish Balay 31e5c89e4eSSatish Balay Note: 32e5c89e4eSSatish Balay This routine is anologous to memcmp() 33e5c89e4eSSatish Balay @*/ 347087cfbeSBarry Smith PetscErrorCode PetscMemcmp(const void *str1,const void *str2,size_t len,PetscBool *e) 35e5c89e4eSSatish Balay { 36e5c89e4eSSatish Balay int r; 37e5c89e4eSSatish Balay 38e5c89e4eSSatish Balay PetscFunctionBegin; 39e32f2f54SBarry Smith if (len > 0 && !str1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to compare at a null pointer"); 40e32f2f54SBarry Smith if (len > 0 && !str2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to compare at a null pointer"); 41e5c89e4eSSatish Balay r = memcmp((char*)str1,(char*)str2,len); 42e5c89e4eSSatish Balay if (!r) *e = PETSC_TRUE; 43e5c89e4eSSatish Balay else *e = PETSC_FALSE; 44e5c89e4eSSatish Balay PetscFunctionReturn(0); 45e5c89e4eSSatish Balay } 46e5c89e4eSSatish Balay 474610e317SSatish Balay /*@ 48e5c89e4eSSatish Balay PetscMemmove - Copies n bytes, beginning at location b, to the space 49e5c89e4eSSatish Balay beginning at location a. Copying between regions that overlap will 50e5c89e4eSSatish Balay take place correctly. 51e5c89e4eSSatish Balay 52e5c89e4eSSatish Balay Not Collective 53e5c89e4eSSatish Balay 54e5c89e4eSSatish Balay Input Parameters: 55e5c89e4eSSatish Balay + b - pointer to initial memory space 56e5c89e4eSSatish Balay - n - length (in bytes) of space to copy 57e5c89e4eSSatish Balay 58e5c89e4eSSatish Balay Output Parameter: 59e5c89e4eSSatish Balay . a - pointer to copy space 60e5c89e4eSSatish Balay 61e5c89e4eSSatish Balay Level: intermediate 62e5c89e4eSSatish Balay 63e5c89e4eSSatish Balay Note: 64e5c89e4eSSatish Balay This routine is analogous to memmove(). 65e5c89e4eSSatish Balay 66300a7f5bSBarry Smith Since b can overlap with a, b cannot be declared as const 67300a7f5bSBarry Smith 68e5c89e4eSSatish Balay Concepts: memory^copying with overlap 69e5c89e4eSSatish Balay Concepts: copying^memory with overlap 70e5c89e4eSSatish Balay 71e5c89e4eSSatish Balay .seealso: PetscMemcpy() 72e5c89e4eSSatish Balay @*/ 737087cfbeSBarry Smith PetscErrorCode PetscMemmove(void *a,void *b,size_t n) 74e5c89e4eSSatish Balay { 75e5c89e4eSSatish Balay PetscFunctionBegin; 76e32f2f54SBarry Smith if (n > 0 && !a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy to null pointer"); 77e32f2f54SBarry Smith if (n > 0 && !b) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Trying to copy from a null pointer"); 78e5c89e4eSSatish Balay #if !defined(PETSC_HAVE_MEMMOVE) 79e5c89e4eSSatish Balay if (a < b) { 80a297a907SKarl Rupp if (a <= b - n) memcpy(a,b,n); 81a297a907SKarl Rupp else { 82e5c89e4eSSatish Balay memcpy(a,b,(int)(b - a)); 83e5c89e4eSSatish Balay PetscMemmove(b,b + (int)(b - a),n - (int)(b - a)); 84e5c89e4eSSatish Balay } 85e5c89e4eSSatish Balay } else { 86a297a907SKarl Rupp if (b <= a - n) memcpy(a,b,n); 87a297a907SKarl Rupp else { 88e5c89e4eSSatish Balay memcpy(b + n,b + (n - (int)(a - b)),(int)(a - b)); 89e5c89e4eSSatish Balay PetscMemmove(a,b,n - (int)(a - b)); 90e5c89e4eSSatish Balay } 91e5c89e4eSSatish Balay } 92e5c89e4eSSatish Balay #else 93e5c89e4eSSatish Balay memmove((char*)(a),(char*)(b),n); 94e5c89e4eSSatish Balay #endif 95e5c89e4eSSatish Balay PetscFunctionReturn(0); 96e5c89e4eSSatish Balay } 97e5c89e4eSSatish Balay 985e71baefSBarry Smith #if defined(PETSC_HAVE_HWLOC) 995e71baefSBarry Smith #include <petsc/private/petscimpl.h> 1005e71baefSBarry Smith #include <hwloc.h> 101e5c89e4eSSatish Balay 1025e71baefSBarry Smith #undef __FUNCT__ 1035e71baefSBarry Smith #define __FUNCT__ "PetscProcessPlacementView" 10442218b76SBarry Smith /*@C 1055e71baefSBarry Smith PetscProcessPlacementView - display the MPI process placement by core 106e5c89e4eSSatish Balay 1075e71baefSBarry Smith Input Parameter: 1085e71baefSBarry Smith . viewer - ASCII viewer to display the results on 1095e71baefSBarry Smith 110*21fcc2ddSBarry Smith Level: intermediate 111*21fcc2ddSBarry Smith 1125e71baefSBarry Smith Notes: Requires that PETSc be installed with hwloc, for example using --download-hwloc 1135e71baefSBarry Smith @*/ 1145e71baefSBarry Smith PetscErrorCode PetscProcessPlacementView(PetscViewer viewer) 1155e71baefSBarry Smith { 1165e71baefSBarry Smith PetscErrorCode ierr; 1175e71baefSBarry Smith PetscBool isascii; 1185e71baefSBarry Smith PetscMPIInt rank; 1195e71baefSBarry Smith hwloc_bitmap_t set; 1205e71baefSBarry Smith hwloc_topology_t topology; 1215e71baefSBarry Smith int err; 1225e71baefSBarry Smith 1235e71baefSBarry Smith PetscFunctionBegin; 1245e71baefSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 1255e71baefSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii); 1265e71baefSBarry Smith if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only ASCII viewer is supported"); 1275e71baefSBarry Smith 1285e71baefSBarry Smith ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 1295e71baefSBarry Smith hwloc_topology_init ( &topology); 1305e71baefSBarry Smith hwloc_topology_load ( topology); 1315e71baefSBarry Smith set = hwloc_bitmap_alloc(); 1325e71baefSBarry Smith 1335e71baefSBarry Smith err = hwloc_get_proc_cpubind(topology, getpid(), set, HWLOC_CPUBIND_PROCESS); 1345e71baefSBarry Smith if (err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error %d from hwloc_get_proc_cpubind()",err); 1355e71baefSBarry Smith ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 1365e71baefSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"MPI rank %d Process id: %d coreid %d\n",rank,getpid(),hwloc_bitmap_first(set));CHKERRQ(ierr); 1375e71baefSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 1385e71baefSBarry Smith hwloc_bitmap_free(set); 1395e71baefSBarry Smith hwloc_topology_destroy(topology); 1405e71baefSBarry Smith PetscFunctionReturn(0); 1415e71baefSBarry Smith } 1425e71baefSBarry Smith #endif 143e5c89e4eSSatish Balay 144