File: CSparse_to_CXSparse

package info (click to toggle)
suitesparse 1%3A5.4.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 138,928 kB
  • sloc: ansic: 389,614; cpp: 24,213; makefile: 5,965; fortran: 1,927; java: 1,808; csh: 1,750; ruby: 725; sh: 226; perl: 225; python: 209; sed: 164; awk: 60
file content (342 lines) | stat: -rwxr-xr-x 9,412 bytes parent folder | download | duplicates (3)
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
#! /usr/bin/perl
# Constructs the CXSparse package from CSparse, adding four different sets of
# functions (int/cs_long_t, and double/complex).  Backward compatible
# with CSparse.  No MATLAB interface is provided for CXSparse, however.
#
# To create CXSparse from CSparse, the ./CXSparse directory should not (yet)
# exist.  Use the following commands, where CSparse is the CSparse directory:
#
#   ./CSparse_to_CXSparse CSparse CXSparse CXSparse_newfiles.tar.gz
#   cd CXSparse/Demo
#   make > cs_demo.out
#
# Alternatively, use "make cx" in the SuiteSparse directory.
#
# Created by David Bateman, Feb. 2006, David dot Bateman atsign motorola dot
# com, and released by him to Tim Davis' copyright.  Modified by Tim Davis,
# 2006-2012, http://www.suitesparse.com.

use strict;
use Cwd;
use File::Find;
use File::Basename;
use Text::Wrap;
use FileHandle;
use IPC::Open3;

my $in_dir = @ARGV[0];
my $out_dir = @ARGV[1];
my $tar_file = @ARGV[2];

#-------------------------------------------------------------------------------
# copy all files from CSparse to CXSparse
#-------------------------------------------------------------------------------

system ("cp -pr $in_dir $out_dir") ;

#-------------------------------------------------------------------------------
# Add the new files from the tar file given by the third argument
#-------------------------------------------------------------------------------

my $old_pwd = cwd();
chdir($out_dir);
system ("tar xpBvzf $old_pwd/$tar_file");
chdir($old_pwd);

#-------------------------------------------------------------------------------
# Convert Demo/* files
#-------------------------------------------------------------------------------

# convert demo *.[ch] files into the four different versions (di, dl, ci, cl)
my @demo_files = ('demo1.c', 'demo2.c', 'demo3.c', 'demo.c', 'demo.h') ;

foreach my $fff ( @demo_files )
{
    my $infile  = sprintf ("%s/Demo/cs_%s", $in_dir, $fff) ;

    # create the plain version
    my $outfile = sprintf ("%s/Demo/cs_%s", $out_dir, $fff) ;
    printf ("%s to %s\n", $infile, $outfile) ;
    if (open (OUT, ">$outfile"))
    {
	if (open (IN, $infile))
	{
	    while (<IN>)
	    {
		# change csi to int
		s/\bcsi\b/int/g;
		print OUT $_;
	    }
	    close (IN);
	}
	close (OUT);
    }

    # create di version
    my $outfile = sprintf ("%s/Demo/cs_di_%s", $out_dir, $fff) ;
    printf ("%s to %s\n", $infile, $outfile) ;
    if (open (OUT, ">$outfile"))
    {
	if (open (IN, $infile))
	{
	    while (<IN>)
	    {
		# change csi to int
		s/\bcsi\b/int/g;
		# change all "cs*" names to "cs_di*", except #include "cs.h"
		s/\bcs/cs_di/g ;
		s/cs_di\.h/cs.h/ ;
		print OUT $_;
	    }
	    close (IN);
	}
	close (OUT);
    }

    # create dl version
    my $outfile = sprintf ("%s/Demo/cs_dl_%s", $out_dir, $fff) ;
    printf ("%s to %s\n", $infile, $outfile) ;
    if (open (OUT, ">$outfile"))
    {
	if (open (IN, $infile))
	{
	    while (<IN>)
	    {
		# change csi to cs_long_t
		s/\bcsi\b/cs_long_t/g;
		# change all "cs*" names to "cs_dl*", except #include "cs.h"
		s/\bcs/cs_dl/g ;
		s/cs_dl_long_t/cs_long_t/g;
		s/cs_dl\.h/cs.h/ ;
		print OUT $_;
	    }
	    close (IN);
	}
	close (OUT);
    }

    # create ci version
    my $outfile = sprintf ("%s/Demo/cs_ci_%s", $out_dir, $fff) ;
    printf ("%s to %s\n", $infile, $outfile) ;
    if (open (OUT, ">$outfile"))
    {
	if (open (IN, $infile))
	{
	    while (<IN>)
	    {
		# change csi to int
		s/\bcsi\b/int/g;
		# change all "cs*" names to "cs_ci*", except #include "cs.h"
		s/\bcs/cs_ci/g ;
		s/cs_ci_long_t/cs_long_t/g;
		s/cs_ci\.h/cs.h/ ;
		# fabs becomes cabs
		s/fabs/cabs/g;
		# change double to cs_complex_t
		s/\bdouble\b/cs_complex_t/g;
		# (double) typecasts stay double
		s/\(cs_complex_t\) /(double) /g;
		# tic, toc, tol, and norm are double, not cs_complex_t
		s/cs_complex_t norm/double norm/;
		s/cs_complex_t tic/double tic/;
		s/cs_complex_t toc \(cs_complex_t/double toc (double/;
		s/cs_complex_t s = tic/double s = tic/;
		s/cs_complex_t tol/double tol/;
		# cumsum, S->lnz, S->unz are double
		s/cs_complex_t lnz/double lnz/;
		s/cs_complex_t unz/double unz/;
		s/cs_complex_t cs_cumsum/double cs_cumsum/;
		# local variable declarations that stay double
		s/,  / ;\n    double / ;
		print OUT $_;
	    }
	    close (IN);
	}
	close (OUT);
    }

    # create cl version
    my $outfile = sprintf ("%s/Demo/cs_cl_%s", $out_dir, $fff) ;
    printf ("%s to %s\n", $infile, $outfile) ;
    if (open (OUT, ">$outfile"))
    {
	if (open (IN, $infile))
	{
	    while (<IN>)
	    {
		# change csi to cs_long_t
		s/\bcsi\b/cs_long_t/g;
		# change all "cs*" names to "cs_cl*", except #include "cs.h"
		s/\bcs/cs_cl/g ;
		s/cs_cl_long_t/cs_long_t/g;
		s/cs_cl\.h/cs.h/ ;
		# fabs becomes cabs
		s/fabs/cabs/g;
		# change double to cs_complex_t
		s/\bdouble\b/cs_complex_t/g;
		# (double) typecasts stay double
		s/\(cs_complex_t\) /(double) /g;
		# tic, toc, tol, and norm are double, not cs_complex_t
		s/cs_complex_t norm/double norm/;
		s/cs_complex_t tic/double tic/;
		s/cs_complex_t toc \(cs_complex_t/double toc (double/;
		s/cs_complex_t s = tic/double s = tic/;
		s/cs_complex_t tol/double tol/;
		# cumsum, S->lnz, S->unz are double
		s/cs_complex_t lnz/double lnz/;
		s/cs_complex_t unz/double unz/;
		s/cs_complex_t cs_cumsum/double cs_cumsum/;
		# local variable declarations that stay double
		s/,  / ;\n    double / ;
		print OUT $_;
	    }
	    close (IN);
	}
	close (OUT);
    }
}

#-------------------------------------------------------------------------------
# Convert Source/*.c files (except cs_house.c, cs_print.c, and cs_load.c)
#-------------------------------------------------------------------------------

# note that cs.h, cs_house.c, cs_updown.c, ...
# are not included in this list
my @src_files = ('Source/cs_add.c', 'Source/cs_amd.c', 'Source/cs_chol.c',
    'Source/cs_cholsol.c', 'Source/cs_counts.c', 'Source/cs_cumsum.c',
    'Source/cs_dfs.c', 'Source/cs_dmperm.c', 'Source/cs_droptol.c',
    'Source/cs_dropzeros.c', 'Source/cs_dupl.c', 'Source/cs_entry.c',
    'Source/cs_etree.c', 'Source/cs_fkeep.c', 'Source/cs_gaxpy.c',
    'Source/cs_happly.c', 'Source/cs_ipvec.c',
    'Source/cs_lsolve.c', 'Source/cs_ltsolve.c', 'Source/cs_lu.c',
    'Source/cs_lusol.c', 'Source/cs_malloc.c', 'Source/cs_maxtrans.c',
    'Source/cs_multiply.c', 'Source/cs_norm.c', 'Source/cs_permute.c',
    'Source/cs_pinv.c', 'Source/cs_post.c',
    'Source/cs_pvec.c', 'Source/cs_qr.c', 'Source/cs_qrsol.c',
    'Source/cs_scatter.c', 'Source/cs_scc.c', 'Source/cs_schol.c',
    'Source/cs_sqr.c', 'Source/cs_symperm.c', 'Source/cs_tdfs.c',
    'Source/cs_transpose.c', 'Source/cs_compress.c',
    'Source/cs_usolve.c', 'Source/cs_util.c', 'Source/cs_utsolve.c',
    'Source/cs_reach.c', 'Source/cs_spsolve.c', 'Source/cs_leaf.c',
    'Source/cs_ereach.c', 'Source/cs_randperm.c' ) ;

foreach my $file ( @src_files )
{
    my $infile  = sprintf ("%s/%s", $in_dir, $file) ;
    my $outfile = sprintf ("%s/%s", $out_dir, $file) ;
    my $fbase = basename($file,('.c'));

    if (open(OUT,">$outfile"))
    {
	if (open(IN,$infile))
	{
	    # my $qrsol_beta_seen = 0;
	    while (<IN>)
	    {

		# change the name of the package (for cs_print.c)
		s/CSparse/CXSparse/g;

		# fabs becomes CS_ABS
		s/fabs/CS_ABS/g;

		# change csi to CS_INT
		s/\bcsi\b/CS_INT/g;

		# change double to CS_ENTRY
		s/\bdouble\b/CS_ENTRY/g;

		# (double) and (double *) typecasts stay double,
		# tol and vnz for cs_vcount stays double
		s/\(CS_ENTRY\) /(double) /g;
		s/\(CS_ENTRY \*\) /(double \*) /;
		s/CS_ENTRY tol/double tol/;
		s/CS_ENTRY \*vnz/double \*vnz/;

		# local variable declarations that stay double
		s/,  / ;\n    double / ;

		#---------------------------------------------------------------
		# Special cases.  Some undo changes made above.
		#---------------------------------------------------------------

		# cs_mex.c
		if ($fbase =~ /cs_mex/)
		{
		    s/matrix must be CS_ENTRY/matrix must be double/;
		    s/A->p =/A->p = (CS_INT *)/;
		    s/A->i =/A->i = (CS_INT *)/;
		    s/, A->p/, (mwIndex *) A->p/;
		    s/, A->i/, (mwIndex *) A->i/;
		}

		# fix comments in cs_add_mex.c and cs_permute_mex.c
		if ($fbase =~ /cs_add_mex/ || $fbase =~ /cs_permute_mex/)
		{
		    s/via CS_ENTRY transpose/via double transpose/;
		}

		# cs_chol
		if ($fbase =~ /cs_chol/)
		{
		    s/\(d <= 0\)/(CS_REAL (d) <= 0 || CS_IMAG (d) != 0)\n\t   /;
		    s/lki \* lki/lki * CS_CONJ (lki)/;
		    s/ = lki/ = CS_CONJ (lki)/;
		}

		# cs_norm
		if ($fbase =~ /cs_norm/)
		{
		    s/^CS_ENTRY cs_norm/double cs_norm/;
		}

		# cs_cumsum
		if ($fbase =~ /cs_cumsum/)
		{
		    s/CS_ENTRY/double/;
		}

		# cs_transpose
		if ($fbase =~ /cs_transpose/)
		{
		    s/Ax \[p\]/(values > 0) ? CS_CONJ (Ax [p]) : Ax [p]/;
		}

		# cs_symperm
		if ($fbase =~ /cs_symperm/)
		{
		    s/Ax \[p\]/(i2 <= j2) ? Ax [p] : CS_CONJ (Ax [p])/;
		}

		# cs_qr
		if ($fbase =~ /cs_qr/)
		{
		    s/n, sizeof \(CS_ENTRY\)/n, sizeof (double)/;
		}

		# cs_happly
		if ($fbase =~ /cs_happly/)
		{
		    s/^(.*tau.*)(Vx\s*\[p\])/$1CS_CONJ ($2)/;
		    s/CS_ENTRY beta/double beta/;
		}

		# cs_ltsolve
		if ($fbase =~ /cs_ltsolve/)
		{
		    s/(Lx \[.*?\])(\s+[\*;])/CS_CONJ ($1)$2/;
		}

		# cs_utsolve
		if ($fbase =~ /cs_utsolve/)
		{
		    s/(Ux \[.*?\])(\s+[\*;])/CS_CONJ ($1)$2/;
		}

		print OUT $_;
	    }
	    close (IN);
	}
	close (OUT);
    }
}