File: exx_file_overlap.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (123 lines) | stat: -rw-r--r-- 2,880 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*----------------------------------------------------------------------
  exx_file_overlap.c 

  Coded by M. Toyoda, 25/NOV/2009
----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "exx.h"
#include "exx_log.h"
#include "exx_file_overlap.h"


#ifdef EXX_USE_MPI
#include <mpi.h>
#endif /* EXX_USE_MPI */

void EXX_File_Overlap_Write(
  int          ndglf,     /* number of double for GLF matrix */
  int          nop_local, /* number of OPs asigned to each node */
  int          nbmax,     /* max number of basis */
  int          jmax,
  double     **buf,       /* [nop_local][ndglf*nbmax*nbmax] */
  const char  *path       /* path to the file */
)
{
  int myrank, nproc, nd1, iop;
  FILE *fp;
  size_t nd, sz;

#ifdef EXX_USE_MPI
  MPI_Comm comm;
  MPI_Status stat;
  double *buf_s;
  int nop_remote, iproc;
#endif /* EXX_USE_MPI */

#ifdef EXX_USE_MPI
  comm = g_exx_mpicomm;
  MPI_Comm_rank(comm, &myrank);
  MPI_Comm_size(comm, &nproc);
#else
  myrank = EXX_ROOT_RANK;
  nproc = 1;
#endif /* EXX_USE_MPI */

  nd = ndglf*nbmax*nbmax; /* length for GLF */

  /* MASTER process */
  if (EXX_ROOT_RANK==myrank) {
    fp = fopen(path, "wb");
    if (NULL==fp) { EXX_ERROR( "failed to open file" ); }
     
    /* save data on master itself */
    for (iop=0; iop<nop_local; iop++) {
      sz = fwrite(buf[iop], sizeof(double), nd, fp);
      if (sz != nd) { EXX_ERROR( "file io failed" ); }
    }

#ifdef EXX_USE_MPI
    /* recieve data from slaves */
    buf_s = (double*)malloc(sizeof(double)*nd);
    for (iproc=1; iproc<nproc; iproc++) {
      MPI_Recv(&nop_remote, 1, MPI_INT, iproc, 0, g_exx_mpicomm, &stat);
      for (iop=0; iop<nop_remote; iop++) {
        MPI_Recv(&buf_s[0], nd, MPI_DOUBLE, iproc, 2*iop+1, comm, &stat);
        sz = fwrite(buf_s, sizeof(double), nd, fp);
        if (sz != nd) { EXX_ERROR( "file io failed" ); }
      }
    }
    free(buf_s);
#endif /* EXX_USE_MPI */

    /* close file */ 
    fclose(fp);
  } 
#ifdef EXX_USE_MPI
  /* SLAVE processes */
  else { 
    MPI_Send(&nop_local, 1, MPI_INT, 0, 0,comm);
    for (iop=0; iop<nop_local; iop++) {
      MPI_Send(buf[iop], nd, MPI_DOUBLE, 0, 2*iop+1, comm);
    }
  }
#endif /* EXX_USE_MPI */  
}




void EXX_File_Overlap_Read(
  int         ndglf,
  int         nbmax,
  int         jmax,
  int         iop,
  double     *buf, /* [ndglf*nbmax*nbmax] */
  const char *path 
)
{
  size_t nd, sz;
  FILE *fp;

  /* open file */
  fp = fopen(path, "rb");
  if (NULL==fp) { EXX_ERROR( "failed to open file" ); }

  nd = ndglf*nbmax*nbmax; /* length for GLF */

  /* move file pointer */
  fseek(fp, iop*nd*sizeof(double), SEEK_SET);
 
  /* read GLF */ 
  sz = fread(buf, sizeof(double), nd, fp);
  if (sz != nd) { EXX_ERROR( "file io error" ); }

  /* close file */
  fclose(fp);
}