-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatlabAPI_implicit.f
More file actions
executable file
·429 lines (412 loc) · 14.9 KB
/
MatlabAPI_implicit.f
File metadata and controls
executable file
·429 lines (412 loc) · 14.9 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
!*************************************************************************************
!
! MATLAB (R) is a trademark of The Mathworks (R) Corporation
!
! Function: MatlabAPI_implicit
! Filename: MatlabAPI_implicit.f
! Programmer: James Tursa
! Version: 1.10
! Date: June 20, 2011
! Copyright: (c) 2009, 2011 by James Tursa, All Rights Reserved
!
! This code uses the BSD License:
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are
! met:
!
! * Redistributions of source code must retain the above copyright
! notice, this list of conditions and the following disclaimer.
! * Redistributions in binary form must reproduce the above copyright
! notice, this list of conditions and the following disclaimer in
! the documentation and/or other materials provided with the distribution
!
! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
! POSSIBILITY OF SUCH DAMAGE.
!
! This function takes no inputs and generates no outputs. What it does is check for
! a design flaw in the Fortran compiler. When a Fortran pointer is passed to a
! routine through an implicit interface, a good compiler will check to see whether
! the target elements are contiguous in memory. If so, then the address of the
! target is passed to the called routine. If not, then a copy-in copy-out scheme
! is used instead. E.g., the Intel Fortran compilers have this desired behavior.
! Some compilers, however (e.g. the Compaq Fortran compilers), will *always* do
! the copy-in copy-out behavior. This is undesireable if you are working with very
! large arrays as the temporary copy typically comes from the stack and you can
! easily overflow the stack and bomb your program.
!
! The output from this routine is simply text output that demonstrates the progress
! of the routine as it is running.
!
! Change Log:
! 2009/Oct/27 --> Initial Release
! 2009/Dec/11 --> Changed default address function to LOC instead of %LOC
! 2011/May/11 --> Added output for specific address information
! --> Changed argument to loc function to be 1st element reference
!
!*************************************************************************************
#include "fintrf.h"
!\
! The following macros are needed for older versions of MATLAB that do not have these
! macros defined in the fintrf.h file.
!/
#ifndef mwSize
#define mwSize integer(4)
#endif
#ifndef mwPointer
#define mwPointer integer(4)
#endif
#ifndef mwIndex
#define mwIndex integer(4)
#endif
!\
! This is in case a compiler supports %LOC but not LOC
!/
#ifdef PERCENTLOC
#define loc %LOC
#endif
!---------------------------------------------------------------------
subroutine mexFunction(nlhs, plhs, nrhs, prhs)
use MatlabAPImex
use MatlabAPImx
implicit none
!-ARG
mwPointer plhs(*), prhs(*)
integer*4 nlhs, nrhs
!-LOC
real(8), target :: X(4,6)
real(8), pointer :: fp(:,:)
mwPointer :: locx, locfp
mwSize :: stride, M, N
character(len=200) :: line
integer(4) :: k
!-COM
common /xblock/ X
!-----
k = mexPrint("--------------------------------------------------")
k = mexPrint("MatlabAPI_implicit test function")
!\
! Check the input
!/
if( nrhs /= 0 ) then
call mexErrMsgTxt("This function has no inputs")
endif
if( nlhs /= 0 ) then
call mexErrMsgTxt("This function returns no outputs")
endif
!\
! Initialize the contiguous pointer.
!/
k = mexPrint(" ")
k = mexPrint("... Initializing X = 1,2,3,...")
call initializeX
k = mexPrint("... Getting contiguous pointer, fp => X")
fp => X
k = mexPrint("... Getting stride of target elements")
stride = fpStride(fp)
locx = loc(X(1,1))
write(line,*) "... LOC(X) = ",locx
k = mexPrint(line(2:))
locfp = loc(fp(1,1))
write(line,*) "... LOC(fp) = ",locfp
k = mexPrint(line(2:))
write(line,*) "... Stride = ",stride
k = mexPrint(line(2:))
if( locx == locfp ) then
k = mexPrint("... fp points to original X")
else
k = mexPrint("... fp points to copy of X")
endif
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
write(line,*) "... fp(:,1) = ",fp(:,1)
k = mexPrint(line(2:))
!\
! Now run the contiguous test, setting values directly
!/
k = mexPrint(" ")
k = mexPrint("... Setting fp slice = 4")
fp = 4.d0
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
write(line,*) "... fp(:,1) = ",fp(:,1)
k = mexPrint(line(2:))
if( any( X /= 4.d0 ) ) then
k = mexPrint("... Original X is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Original X is changed.")
endif
k = mexPrint(" ")
k = mexPrint("... Setting X = 5")
X = 5.d0
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
write(line,*) "... fp(:,1) = ",fp(:,1)
k = mexPrint(line(2:))
if( any( fp /= 5.d0 ) ) then
k = mexPrint("... Pointer fp target is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Pointer fp target is changed.")
endif
!\
! Now run the contiguous test, pass the pointer implicitly
!/
k = mexPrint(" ")
k = mexPrint("... Initializing X = 1,2,3,...")
call initializeX
k = mexPrint("... call implicit(X,4,6,LOC(X))")
M = 4
N = 6
call implicit(X,M,N,locx)
if( any( fp /= 1.d0 ) ) then
k = mexPrint("... Pointer fp target is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Pointer fp target is changed.")
endif
k = mexPrint(" ")
k = mexPrint("... Initializing X = 1,2,3,...")
call initializeX
k = mexPrint("... call implicit(fp,4,6,LOC(fp))")
M = 4
N = 6
call implicit(fp,M,N,locfp)
if( any( X /= 1.d0 ) ) then
k = mexPrint("... Original X is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Original X is changed as expected.")
endif
k = mexPrint(" ")
k = mexPrint("... Initializing X = 1,2,3,...")
call initializeX
k = mexPrint("... call implicit(VAL(LOC(fp)),4,6,LOC(fp))")
M = 4
N = 6
call implicit(%VAL(loc(fp(1,1))),M,N,locfp)
if( any( X /= 1.d0 ) ) then
k = mexPrint("... Original X is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Original X is changed as expected.")
endif
!\
! Initialize the non-contiguous pointer.
!/
k = mexPrint(" ")
k = mexPrint("... Initializing X = 1,2,3,...")
call initializeX
k = mexPrint(" ")
k = mexPrint("... Getting non-contiguous pointer fp => X(::2,:)")
fp => X(::2,:)
locx = loc(X(1,1))
write(line,*) "... LOC(X) = ",locx
k = mexPrint(line(2:))
locfp = loc(fp(1,1))
write(line,*) "... LOC(fp) = ",locfp
k = mexPrint(line(2:))
stride = fpStride(fp)
write(line,*) "... Stride = ",stride
k = mexPrint(line(2:))
if( locx == locfp ) then
k = mexPrint("... fp points to original X")
else
k = mexPrint("... fp points to copy of X")
endif
write(line,*) "... X shape = ",size(X,1)," x ",size(X,2)
k = mexPrint(line(2:))
write(line,*) "... fp shape = ",size(fp,1)," x ",size(fp,2)
k = mexPrint(line(2:))
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
write(line,*) "... fp(:,1) = ",fp(:,1)
k = mexPrint(line(2:))
!\
! Now run the non-contiguous test, setting values directly
!/
k = mexPrint(" ")
k = mexPrint("... Setting fp slice = 3")
fp = 3.d0
if( any( X(::2,:) /= 3.d0 ) ) then
k = mexPrint("... Original X is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Original X is changed.")
endif
k = mexPrint(" ")
k = mexPrint("... Setting X = 5")
X = 5.d0
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
write(line,*) "... fp(:,1) = ",fp(:,1)
k = mexPrint(line(2:))
if( any( fp /= 5.d0 ) ) then
k = mexPrint("... Pointer fp target is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Pointer fp target is changed.")
endif
!\
! Now run the non-contiguous test, pass the pointer implicitly
!/
k = mexPrint(" ")
k = mexPrint("... Initializing X = 1,2,3,...")
call initializeX
k = mexPrint("... call implicit2(fp,2,6,LOC(fp))")
M = 2
N = 6
call implicit2(fp,M,N,locfp)
if( any( X(::2,:) /= 2.d0 ) ) then
k = mexPrint("... Original X is not changed.")
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("... Original X is changed.")
endif
!\
! Done
!/
k = mexPrint("--------------------------------------------------")
k = mexPrint(" ")
k = mexPrint("... Done")
k = mexPrint(" ")
return
contains
!----------------------------------------------------------------------------
subroutine initializeX
X(:,1) = (/ 1.d0, 2.d0, 3.d0, 4.d0 /)
X(:,2) = (/ 5.d0, 6.d0, 7.d0, 8.d0 /)
X(:,3) = (/ 9.d0, 9.d0, 10.d0, 11.d0 /)
X(:,4) = (/ 13.d0, 14.d0, 15.d0, 16.d0 /)
X(:,5) = (/ 17.d0, 18.d0, 19.d0, 20.d0 /)
X(:,6) = (/ 21.d0, 22.d0, 23.d0, 24.d0 /)
return
end subroutine initializeX
end subroutine mexFunction
!----------------------------------------------------------------------------
!\
! The following routines, since they are not contained in another programming
! unit, have an implicit interface. As such, they cannot use assumed shape
! arguments and one is forced to pass the sizes manually.
!/
!----------------------------------------------------------------------------
subroutine Implicit(Y,M,N,address)
use MatlabAPImex
implicit none
!-ARG
mwSize, intent(in) :: M, N
real(8), intent(inout) :: Y(M,N) ! Passed size
mwPointer, intent(in) :: address
!-LOC
mwPointer :: locy
integer(4) :: k
character(len=200) :: line
!-COM
real(8) :: X(4,6)
common /xblock/ X
!-----
k = mexPrint("Inside subroutine Implicit, contiguous case")
write(line,*) "... Y shape = ",size(Y,1)," x ",size(Y,2)
k = mexPrint(line(2:))
locy = loc(Y(1,1))
write(line,*) "... LOC(Y) = ",locy
k = mexPrint(line(2:))
write(line,*) "... LOC(fp) = ",address
k = mexPrint(line(2:))
write(line,*) "... Y(:,1) = ",Y(:,1)
k = mexPrint(line(2:))
if( locy == address ) then
k = mexPrint("Good, Address of Target was passed")
else
k = mexPrint("Bad, Address of Copy was passed.")
k = mexPrint("Your compiler, although conforming, has a")
k = mexPrint("design flaw for passing contiguous targets.")
k = mexPrint(" ")
k = mexPrint("To get around this limitation, particularly")
k = mexPrint("when working with vary large arrays, you")
k = mexPrint("can use the following construct when calling")
k = mexPrint("routines with implicit interfaces:")
k = mexPrint("use MatlabAPImx")
k = mexPrint("if( fpStride(fp) == 1 ) then")
k = mexPrint(" call routine(VAL(LOC(fp(1,1))),...)")
k = mexPrint("else")
k = mexPrint(" call routine(fp,...)")
k = mexPrint("endif")
k = mexPrint(" ")
k = mexPrint("Basically, the fpStride function returns a")
k = mexPrint("1 if all the elements of the target are")
k = mexPrint("contiguous in memory. If the stride is other")
k = mexPrint("than 1 then fpStride will return that number")
k = mexPrint("instead. Or if there is no consistent stride")
k = mexPrint("in the target then fpStride will return 0.")
k = mexPrint(" ")
endif
k = mexPrint("... Setting Y = 1")
Y = 1.d0
write(line,*) "... Y(:,1) = ",Y(:,1)
k = mexPrint(line(2:))
k = mexPrint("... Checking to see if original X changed.")
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
if( any( X /= Y ) ) then
k = mexPrint("... Original X is not changed yet.")
else
k = mexPrint("... Original X is changed.")
endif
return
end subroutine Implicit
!----------------------------------------------------------------------
subroutine Implicit2(Y,M,N,address)
use MatlabAPImex
implicit none
!-ARG
mwSize, intent(in) :: M, N
real(8), intent(inout) :: Y(M,N) ! Passed size
mwPointer, intent(in) :: address
!-LOC
mwPointer :: locy
integer(4) :: k
character(len=200) :: line
!-COM
real(8) :: X(4,6)
common /xblock/ X
!-----
k = mexPrint("Inside subroutine Implicit2, non-contiguous case")
write(line,*) "... Y shape = ",size(Y,1)," x ",size(Y,2)
k = mexPrint(line(2:))
locy = loc(Y(1,1))
write(line,*) "... LOC(Y) = ",locy
k = mexPrint(line(2:))
write(line,*) "... address = ",address
k = mexPrint(line(2:))
write(line,*) "... Y(:,1) = ",Y(:,1)
k = mexPrint(line(2:))
if( locy == address ) then
k = mexPrint("BAD UNEXPECTED RESULT. Contact author.")
else
k = mexPrint("Expected Result, Address of Copy was passed")
endif
k = mexPrint("... Setting Y = 2")
Y = 2.d0
write(line,*) "... Y(:,1) = ",Y(:,1)
k = mexPrint(line(2:))
k = mexPrint("... Checking to see if original X changed.")
write(line,*) "... X(:,1) = ",X(:,1)
k = mexPrint(line(2:))
if( any( X(::2,:) /= Y ) ) then
k = mexPrint("... Original X is not changed yet.")
else
k = mexPrint("... Original X is changed.")
endif
return
end subroutine Implicit2