1 /*
2  * Copyright (c) 2003, 2007-14 Matteo Frigo
3  * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 #include "mpi-rdft.h"
22 
destroy(problem * ego_)23 static void destroy(problem *ego_)
24 {
25      problem_mpi_rdft *ego = (problem_mpi_rdft *) ego_;
26      XM(dtensor_destroy)(ego->sz);
27      MPI_Comm_free(&ego->comm);
28 #if !defined(STRUCT_HACK_C99) && !defined(STRUCT_HACK_KR)
29      X(ifree0)(ego->kind);
30 #endif
31      X(ifree)(ego_);
32 }
33 
hash(const problem * p_,md5 * m)34 static void hash(const problem *p_, md5 *m)
35 {
36      const problem_mpi_rdft *p = (const problem_mpi_rdft *) p_;
37      int i;
38      X(md5puts)(m, "mpi-dft");
39      X(md5int)(m, p->I == p->O);
40      /* don't include alignment -- may differ between processes
41 	X(md5int)(m, X(ialignment_of)(p->I));
42 	X(md5int)(m, X(ialignment_of)(p->O));
43 	... note that applicability of MPI plans does not depend
44 	    on alignment (although optimality may, in principle). */
45      XM(dtensor_md5)(m, p->sz);
46      X(md5INT)(m, p->vn);
47      for (i = 0; i < p->sz->rnk; ++i)
48 	  X(md5int)(m, p->kind[i]);
49      X(md5int)(m, p->flags);
50      MPI_Comm_size(p->comm, &i); X(md5int)(m, i);
51      A(XM(md5_equal)(*m, p->comm));
52 }
53 
print(const problem * ego_,printer * p)54 static void print(const problem *ego_, printer *p)
55 {
56      const problem_mpi_rdft *ego = (const problem_mpi_rdft *) ego_;
57      int i;
58      p->print(p, "(mpi-rdft %d %d %d ",
59 	      ego->I == ego->O,
60 	      X(ialignment_of)(ego->I),
61 	      X(ialignment_of)(ego->O));
62      XM(dtensor_print)(ego->sz, p);
63      for (i = 0; i < ego->sz->rnk; ++i)
64           p->print(p, " %d", (int)ego->kind[i]);
65      p->print(p, " %D %d", ego->vn, ego->flags);
66      MPI_Comm_size(ego->comm, &i); p->print(p, " %d)", i);
67 }
68 
zero(const problem * ego_)69 static void zero(const problem *ego_)
70 {
71      const problem_mpi_rdft *ego = (const problem_mpi_rdft *) ego_;
72      R *I = ego->I;
73      INT i, N;
74      int my_pe;
75 
76      MPI_Comm_rank(ego->comm, &my_pe);
77      N = ego->vn * XM(total_block)(ego->sz, IB, my_pe);
78      for (i = 0; i < N; ++i) I[i] = K(0.0);
79 }
80 
81 static const problem_adt padt =
82 {
83      PROBLEM_MPI_RDFT,
84      hash,
85      zero,
86      print,
87      destroy
88 };
89 
XM(mkproblem_rdft)90 problem *XM(mkproblem_rdft)(const dtensor *sz, INT vn,
91 			    R *I, R *O,
92 			    MPI_Comm comm,
93 			    const rdft_kind *kind, unsigned flags)
94 {
95      problem_mpi_rdft *ego;
96      int i, rnk = sz->rnk;
97      int n_pes;
98 
99      A(XM(dtensor_validp)(sz) && FINITE_RNK(sz->rnk));
100      MPI_Comm_size(comm, &n_pes);
101      A(n_pes >= XM(num_blocks_total)(sz, IB)
102        && n_pes >= XM(num_blocks_total)(sz, OB));
103      A(vn >= 0);
104 
105 #if defined(STRUCT_HACK_KR)
106      ego = (problem_mpi_rdft *) X(mkproblem)(sizeof(problem_mpi_rdft)
107 					     + sizeof(rdft_kind)
108 					     * (rnk > 0 ? rnk - 1 : 0), &padt);
109 #elif defined(STRUCT_HACK_C99)
110      ego = (problem_mpi_rdft *) X(mkproblem)(sizeof(problem_mpi_rdft)
111 					     + sizeof(rdft_kind) * rnk, &padt);
112 #else
113      ego = (problem_mpi_rdft *) X(mkproblem)(sizeof(problem_mpi_rdft), &padt);
114      ego->kind = (rdft_kind *) MALLOC(sizeof(rdft_kind) * rnk, PROBLEMS);
115 #endif
116 
117      /* enforce pointer equality if untainted pointers are equal */
118      if (UNTAINT(I) == UNTAINT(O))
119 	  I = O = JOIN_TAINT(I, O);
120 
121      ego->sz = XM(dtensor_canonical)(sz, 0);
122      ego->vn = vn;
123      ego->I = I;
124      ego->O = O;
125      for (i = 0; i< ego->sz->rnk; ++i)
126 	  ego->kind[i] = kind[i];
127 
128      /* canonicalize: replace TRANSPOSED_IN with TRANSPOSED_OUT by
129         swapping the first two dimensions (for rnk > 1) */
130      if ((flags & TRANSPOSED_IN) && ego->sz->rnk > 1) {
131 	  rdft_kind k = ego->kind[0];
132 	  ddim dim0 = ego->sz->dims[0];
133 	  ego->sz->dims[0] = ego->sz->dims[1];
134 	  ego->sz->dims[1] = dim0;
135 	  ego->kind[0] = ego->kind[1];
136 	  ego->kind[1] = k;
137 	  flags &= ~TRANSPOSED_IN;
138 	  flags ^= TRANSPOSED_OUT;
139      }
140      ego->flags = flags;
141 
142      MPI_Comm_dup(comm, &ego->comm);
143 
144      return &(ego->super);
145 }
146 
XM(mkproblem_rdft_d)147 problem *XM(mkproblem_rdft_d)(dtensor *sz, INT vn,
148 			      R *I, R *O,
149 			      MPI_Comm comm,
150 			      const rdft_kind *kind, unsigned flags)
151 {
152      problem *p = XM(mkproblem_rdft)(sz, vn, I, O, comm, kind, flags);
153      XM(dtensor_destroy)(sz);
154      return p;
155 }
156