xref: /petsc/src/sys/utils/memc.c (revision 5e71baeff2f3138f93cd4f5927dfd596eb8325cc)
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 
98*5e71baefSBarry Smith #if defined(PETSC_HAVE_HWLOC)
99*5e71baefSBarry Smith #include <petsc/private/petscimpl.h>
100*5e71baefSBarry Smith #include <hwloc.h>
101e5c89e4eSSatish Balay 
102*5e71baefSBarry Smith #undef __FUNCT__
103*5e71baefSBarry Smith #define __FUNCT__ "PetscProcessPlacementView"
104*5e71baefSBarry Smith /*@
105*5e71baefSBarry Smith      PetscProcessPlacementView - display the MPI process placement by core
106e5c89e4eSSatish Balay 
107*5e71baefSBarry Smith   Input Parameter:
108*5e71baefSBarry Smith .   viewer - ASCII viewer to display the results on
109*5e71baefSBarry Smith 
110*5e71baefSBarry Smith   Notes: Requires that PETSc be installed with hwloc, for example using --download-hwloc
111*5e71baefSBarry Smith @*/
112*5e71baefSBarry Smith PetscErrorCode PetscProcessPlacementView(PetscViewer viewer)
113*5e71baefSBarry Smith {
114*5e71baefSBarry Smith   PetscErrorCode   ierr;
115*5e71baefSBarry Smith   PetscBool        isascii;
116*5e71baefSBarry Smith   PetscMPIInt      rank;
117*5e71baefSBarry Smith   hwloc_bitmap_t   set;
118*5e71baefSBarry Smith   hwloc_topology_t topology;
119*5e71baefSBarry Smith   int              err;
120*5e71baefSBarry Smith 
121*5e71baefSBarry Smith   PetscFunctionBegin;
122*5e71baefSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
123*5e71baefSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
124*5e71baefSBarry Smith   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Only ASCII viewer is supported");
125*5e71baefSBarry Smith 
126*5e71baefSBarry Smith   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr);
127*5e71baefSBarry Smith   hwloc_topology_init ( &topology);
128*5e71baefSBarry Smith   hwloc_topology_load ( topology);
129*5e71baefSBarry Smith   set = hwloc_bitmap_alloc();
130*5e71baefSBarry Smith 
131*5e71baefSBarry Smith   err = hwloc_get_proc_cpubind(topology, getpid(), set, HWLOC_CPUBIND_PROCESS);
132*5e71baefSBarry Smith   if (err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error %d from hwloc_get_proc_cpubind()",err);
133*5e71baefSBarry Smith   ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
134*5e71baefSBarry Smith   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"MPI rank %d Process id: %d coreid %d\n",rank,getpid(),hwloc_bitmap_first(set));CHKERRQ(ierr);
135*5e71baefSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
136*5e71baefSBarry Smith   hwloc_bitmap_free(set);
137*5e71baefSBarry Smith   hwloc_topology_destroy(topology);
138*5e71baefSBarry Smith   PetscFunctionReturn(0);
139*5e71baefSBarry Smith }
140*5e71baefSBarry Smith #endif
141e5c89e4eSSatish Balay 
142