SCIP-SDP  2.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
lapack_sdpa.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of SCIPSDP - a solving framework for mixed-integer */
4 /* semidefinite programms based on SCIP. */
5 /* */
6 /* Copyright (C) 2011-2013 Discrete Optimization, TU Darmstadt */
7 /* EDOM, FAU Erlangen-Nürnberg */
8 /* 2014-2016 Discrete Optimization, TU Darmstadt */
9 /* */
10 /* */
11 /* This program is free software; you can redistribute it and/or */
12 /* modify it under the terms of the GNU Lesser General Public License */
13 /* as published by the Free Software Foundation; either version 3 */
14 /* of the License, or (at your option) any later version. */
15 /* */
16 /* This program is distributed in the hope that it will be useful, */
17 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
18 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
19 /* GNU Lesser General Public License for more details. */
20 /* */
21 /* You should have received a copy of the GNU Lesser General Public License */
22 /* along with this program; if not, write to the Free Software */
23 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.*/
24 /* */
25 /* */
26 /* Based on SCIP - Solving Constraint Integer Programs */
27 /* Copyright (C) 2002-2016 Zuse Institute Berlin */
28 /* SCIP is distributed under the terms of the SCIP Academic Licence, */
29 /* see file COPYING in the SCIP distribution. */
30 /* */
31 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
32 
33 /* #define SCIP_DEBUG*/
34 /* #define SCIP_MORE_DEBUG*/
35 
43 #include <assert.h>
44 
45 #include "lapack.h"
46 #include "config.h" /* for F77_FUNC */
47 
48 #include "scip/def.h"
49 #include "blockmemshell/memory.h"
50 #include "scip/type_retcode.h"
51 
52 /* turn off lint warnings for whole file: */
53 /*lint --e{788,818}*/
54 
55 typedef long long int LAPACKINTTYPE;
56 
58 #define BMS_CALL(x) do \
59  { \
60  if( NULL == (x) ) \
61  { \
62  SCIPerrorMessage("No memory in function call\n"); \
63  return SCIP_NOMEMORY; \
64  } \
65  } \
66  while( FALSE )
67 
69 #define SCIP_RealTOINT(x) ((LAPACKINTTYPE) (x + 0.5))
70 
71 /*
72  * BLAS/LAPACK Calls
73  */
74 
79 void F77_FUNC(dsyevr, DSYEVR)(
80  char* JOBZ, char* RANGE, char* UPLO,
81  LAPACKINTTYPE* N, SCIP_Real* A, LAPACKINTTYPE* LDA,
82  SCIP_Real* VL, SCIP_Real* VU,
84  SCIP_Real* ABSTOL, LAPACKINTTYPE* M, SCIP_Real* W, SCIP_Real* Z,
85  LAPACKINTTYPE* LDZ, LAPACKINTTYPE* ISUPPZ, SCIP_Real* WORK,
86  LAPACKINTTYPE* LWORK, LAPACKINTTYPE* IWORK, LAPACKINTTYPE* LIWORK,
87  int* INFO );
88 
89 
91 void F77_FUNC(dgemv, DGEMV)(char* TRANS, LAPACKINTTYPE* M,
92  LAPACKINTTYPE* N, SCIP_Real* ALPHA, SCIP_Real* A, LAPACKINTTYPE* LDA,
93  SCIP_Real* X, LAPACKINTTYPE* INCX, SCIP_Real* BETA, SCIP_Real* Y, LAPACKINTTYPE* INCY);
94 
95 
99 /*
100  * Functions
101  */
102 
107 SCIP_RETCODE SCIPlapackComputeIthEigenvalue(
108  BMS_BLKMEM* blkmem,
109  SCIP_Bool geteigenvectors,
110  int n,
111  SCIP_Real* A,
112  int i,
113  SCIP_Real* eigenvalue,
114  SCIP_Real* eigenvector
115  )
116 {
117  LAPACKINTTYPE N;
118  int INFO;
119  char JOBZ;
120  char RANGE;
121  char UPLO;
122  LAPACKINTTYPE LDA;
123  SCIP_Real* WORK;
124  LAPACKINTTYPE LWORK;
125  LAPACKINTTYPE* IWORK;
126  LAPACKINTTYPE LIWORK;
127  SCIP_Real* WTMP;
128  SCIP_Real ABSTOL;
129  LAPACKINTTYPE IL;
130  LAPACKINTTYPE IU;
131  LAPACKINTTYPE M;
132  LAPACKINTTYPE LDZ;
133  SCIP_Real WSIZE;
134  LAPACKINTTYPE WISIZE;
135  SCIP_Real VL;
136  SCIP_Real VU;
137  LAPACKINTTYPE* ISUPPZ;
138 
139  assert( blkmem != NULL );
140  assert( n > 0 );
141  assert( A != NULL );
142  assert( 0 < i && i <= n );
143  assert( eigenvalue != NULL );
144  assert( ( ! geteigenvectors) || eigenvector != NULL );
145 
146  N = n;
147  JOBZ = geteigenvectors ? 'V' : 'N';
148  RANGE = 'I';
149  UPLO = 'L';
150  LDA = n;
151  ABSTOL = 0.0;
152  IL = i;
153  IU = i;
154  M = 1;
155  LDZ = n;
156 
157  /* standard LAPACK workspace query, to get the amount of needed memory */
158  LWORK = -1LL;
159  LIWORK = -1LL;
160 
161  /* this computes the internally needed memory and returns this as (the first entries of [the 1x1 arrays]) WSIZE and WISIZE */
162  F77_FUNC(dsyevr, DSYEVR)( &JOBZ, &RANGE, &UPLO,
163  &N, NULL, &LDA,
164  NULL, NULL,
165  &IL, &IU,
166  &ABSTOL, &M, NULL, NULL,
167  &LDZ, NULL, &WSIZE,
168  &LWORK, &WISIZE, &LIWORK,
169  &INFO );
170 
171  if ( INFO != 0 )
172  {
173  SCIPerrorMessage("There was an error when calling DSYEVR. INFO = %d\n", INFO);
174  return SCIP_ERROR;
175  }
176 
177  /* allocate workspace */
178  LWORK = SCIP_RealTOINT(WSIZE);
179  LIWORK = WISIZE;
180 
181  BMS_CALL( BMSallocBlockMemoryArray(blkmem, &WORK, (int) LWORK) );
182  BMS_CALL( BMSallocBlockMemoryArray(blkmem, &IWORK, (int) LIWORK) );
183  BMS_CALL( BMSallocBlockMemoryArray(blkmem, &WTMP, (int) N) );
184  BMS_CALL( BMSallocBlockMemoryArray(blkmem, &ISUPPZ, 2) ); /*lint !e506*/
185 
186  /* call the function */
187  VL = -1e20;
188  VU = 1e20;
189 
190  F77_FUNC(dsyevr, DSYEVR)( &JOBZ, &RANGE, &UPLO,
191  &N, A, &LDA,
192  &VL, &VU,
193  &IL, &IU,
194  &ABSTOL, &M, WTMP, eigenvector,
195  &LDZ, ISUPPZ, WORK,
196  &LWORK, IWORK, &LIWORK,
197  &INFO );
198 
199  if ( INFO != 0 )
200  {
201  SCIPerrorMessage("There was an error when calling DSYEVR. INFO = %d\n", INFO);
202  return SCIP_ERROR;
203  }
204 
205  /* handle output */
206  *eigenvalue = WTMP[0];
207 
208  /* free memory */
209  BMSfreeBlockMemoryArray(blkmem, &ISUPPZ, 2);
210  BMSfreeBlockMemoryArray(blkmem, &WTMP, (int) N);/*lint !e737*/
211  BMSfreeBlockMemoryArray(blkmem, &IWORK, (int) LIWORK);/*lint !e737*/
212  BMSfreeBlockMemoryArray(blkmem, &WORK, (int) LWORK);/*lint !e737*/
213 
214  return SCIP_OKAY;
215 }
216 
219  int nrows,
220  int ncols,
221  SCIP_Real* matrix,
222  SCIP_Real* vector,
223  SCIP_Real* result
224  )
225 {
226  char TRANS;
227  LAPACKINTTYPE M;
228  LAPACKINTTYPE N;
229  SCIP_Real ALPHA;
230  SCIP_Real* A;
231  LAPACKINTTYPE LDA;
232  SCIP_Real* X;
233  LAPACKINTTYPE INCX;
234  SCIP_Real BETA;
235  SCIP_Real* Y;
236  LAPACKINTTYPE INCY;
237 
238  TRANS = 'N';
239  M = nrows;
240  N = ncols;
241  ALPHA = 1.0;
242  A = matrix;
243  LDA = nrows;
244  X = vector;
245  INCX = 1;
246  BETA = 0.0;
247  Y = result;
248  INCY = 1;
249 
250  F77_FUNC(dgemv, DGEMV)(&TRANS, &M, &N, &ALPHA, A, &LDA, X, &INCX, &BETA, Y, &INCY);
251 
252  return SCIP_OKAY;
253 }
254 
long long int LAPACKINTTYPE
Definition: lapack_sdpa.c:55
Macros for calling Fortran-functions.
SCIP_RETCODE SCIPlapackMatrixVectorMult(int nrows, int ncols, SCIP_Real *matrix, SCIP_Real *vector, SCIP_Real *result)
Definition: lapack_sdpa.c:218
#define BMS_CALL(x)
Definition: lapack_sdpa.c:58
#define SCIP_RealTOINT(x)
Definition: lapack_sdpa.c:69
#define F77_FUNC(name, NAME)
Definition: config.h:43
interface methods for eigenvector computation and matrix multiplication using different versions of L...
EXTERN SCIP_RETCODE SCIPlapackComputeIthEigenvalue(BMS_BLKMEM *blkmem, SCIP_Bool geteigenvectors, int n, SCIP_Real *A, int i, SCIP_Real *eigenvalue, SCIP_Real *eigenvector)