~ubuntu-branches/ubuntu/natty/jigl/natty

« back to all changes in this revision

Viewing changes to debian/vs-multicpu.patch

  • Committer: Bazaar Package Importer
  • Author(s): Nicholas Breen
  • Date: 2006-10-08 18:50:23 UTC
  • Revision ID: james.westby@ubuntu.com-20061008185023-jlvcr6pt1t9okbnm
Tags: 2.0.1+20060126-2
* New maintainer.  (Closes: #390865)
* Incorporate Vincent Stehle's patch for multi-CPU support.
* Add manpages.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--- jigl        2006-01-27 12:15:03.000000000 +0100
 
2
+++ jigl        2006-05-29 01:27:36.000000000 +0200
 
3
@@ -20,6 +20,7 @@
 
4
 #
 
5
 ####################
 
6
 # Author: Jason Paul
 
7
+# Parallel computation by Vincent Stehl� <vincent.stehle@free.fr>
 
8
 
 
9
 use Getopt::Long;       # options parsing
 
10
 use Text::ParseWords;   # for converting the config options into an array
 
11
@@ -63,6 +64,17 @@
 
12
 my $gblInfoTmpl  = $gblThemeDir . "/default/" . $infoTmpl;  # global info template
 
13
 my @imgMgkVer    = &checkSiteInstall;  # check for img tools; return ImageMagick ver
 
14
 
 
15
+###########################################################
 
16
+#
 
17
+# MP_MAX_JOBS
 
18
+#
 
19
+# Description:
 
20
+#   Maximum number of parallel jobs to run at most.
 
21
+#   We set this to the number of cpu in the system.
 
22
+#
 
23
+###########################################################
 
24
+my $MP_MAX_JOBS = mp_detect_cpu();
 
25
+
 
26
 ######################
 
27
 ### end variable setup
 
28
 ######################
 
29
@@ -343,6 +355,161 @@
 
30
 }
 
31
 
 
32
 ###########################################################
 
33
+#
 
34
+# mp_jobs
 
35
+#
 
36
+# Description:
 
37
+#   Temporary hash, to track the running jobs. Keys are
 
38
+#   the jobs pids. Values are the message to print in case
 
39
+#   of non-zero return code, when die-ing.
 
40
+#
 
41
+###########################################################
 
42
+my %mp_jobs;
 
43
+
 
44
+###########################################################
 
45
+#
 
46
+# mp_debug
 
47
+#
 
48
+# Description:
 
49
+#   Debug printing for mp_ functions.
 
50
+#
 
51
+###########################################################
 
52
+sub mp_debug($)
 
53
+{
 
54
+#    print "mp[$$]: ". $_[0];
 
55
+}
 
56
+
 
57
+###########################################################
 
58
+#
 
59
+# mp_detect_cpu
 
60
+#
 
61
+# Description:
 
62
+#   Detect the number of cpu present in the system.
 
63
+#
 
64
+# Fixme:
 
65
+#   This is Linux only for now.
 
66
+#   This could be more eleguant.
 
67
+#
 
68
+###########################################################
 
69
+sub mp_detect_cpu
 
70
+{
 
71
+    my $r = `grep processor /proc/cpuinfo |wc -l`;
 
72
+    mp_debug("Detected $r cpu.\n");
 
73
+    return $r;
 
74
+}
 
75
+
 
76
+###########################################################
 
77
+#
 
78
+# mp_wait_jobs
 
79
+#
 
80
+# Description:
 
81
+#   Wait for children until there are n running jobs
 
82
+#   or less.
 
83
+#
 
84
+###########################################################
 
85
+sub mp_wait_jobs($)
 
86
+{
 
87
+    my($n) = @_;
 
88
+
 
89
+    while(1){
 
90
+        my $running = scalar(keys %mp_jobs);
 
91
+        mp_debug("$running job(s) currently running.\n");
 
92
+        
 
93
+        last if $running <= $n;
 
94
+
 
95
+        mp_debug("Waiting...\n");
 
96
+
 
97
+        # Wait for a job to finish.
 
98
+        my $c = wait();
 
99
+        my $rc = $?;
 
100
+        mp_debug("-> $c ($rc)\n");
 
101
+
 
102
+        # Sanity checks.
 
103
+        if($c < 0){
 
104
+            print "mp: wait() returned $c. Strange!\n";
 
105
+        }
 
106
+
 
107
+        if(!exists $mp_jobs{$c}){
 
108
+            print "mp: wait() returned $c, but job is not"
 
109
+                    ." in mp_jobs. Strange!\n";
 
110
+        }
 
111
+
 
112
+        # Check the return code and die if non-zero.
 
113
+        die $mp_jobs{$c} if $rc;
 
114
+
 
115
+        # Forget job.
 
116
+        delete $mp_jobs{$c};
 
117
+    }
 
118
+}
 
119
+
 
120
+###########################################################
 
121
+#
 
122
+# mp_wait_for_free_slot
 
123
+#
 
124
+# Description:
 
125
+#   Wait for a free slot. This is done before launching
 
126
+#   one more job in parallel.
 
127
+#
 
128
+###########################################################
 
129
+sub mp_wait_for_free_slot()
 
130
+{
 
131
+    mp_wait_jobs($MP_MAX_JOBS - 1);
 
132
+}
 
133
+
 
134
+###########################################################
 
135
+#
 
136
+# mp_wait_for_all_jobs
 
137
+#
 
138
+# Description:
 
139
+#   Wait for all children until there are no more running.
 
140
+#
 
141
+###########################################################
 
142
+sub mp_wait_for_all_jobs()
 
143
+{
 
144
+    mp_wait_jobs(0);
 
145
+}
 
146
+
 
147
+###########################################################
 
148
+#
 
149
+# mp_launch
 
150
+#
 
151
+# Description:
 
152
+#   Like "system", but forked in parallel.
 
153
+#
 
154
+# Arguments:
 
155
+#   $_[0]: Command to launch.
 
156
+#   $_[1]: Die message, in case of non-zero return code.
 
157
+#
 
158
+# Notes:
 
159
+#   We ensure not to exceed the maximum number of slots
 
160
+#   allowed.
 
161
+#
 
162
+###########################################################
 
163
+sub mp_launch($$)
 
164
+{
 
165
+    my($cmd, $die_msg) = @_;
 
166
+
 
167
+    # Wait for a free slot first.
 
168
+    mp_wait_for_free_slot();
 
169
+
 
170
+    # Ok, fork.
 
171
+    my $r = fork();
 
172
+    
 
173
+    if(!$r){
 
174
+        # We are in the child.
 
175
+        # Do the job.
 
176
+        mp_debug("Running $cmd.\n");
 
177
+        exec($cmd);
 
178
+
 
179
+    } else {
 
180
+        # We are in the father.
 
181
+        # Remember the child as well as the die msg.
 
182
+        mp_debug("Launched $r.\n");
 
183
+        $mp_jobs{$r} = $die_msg;
 
184
+    }
 
185
+}
 
186
+
 
187
+###########################################################
 
188
 # getWatermarkProg - determine what program we have that can watermark.
 
189
 # older versions of ImageMagick use the program 'combine'. Newer versions
 
190
 # use 'composite'. We'll check and see which one you have installed.
 
191
@@ -1216,6 +1383,8 @@
 
192
     }
 
193
 
 
194
     # generate thumbnail for each image
 
195
+    # First pass: scale all images. This is done in parallel
 
196
+    # if possible, with jobs launched in the background.
 
197
     for $i (0 .. $#{$albumInfo->{images}}) {
 
198
         # get the name of the file and create the thumbnail filename
 
199
         # store the name of the thumbnail for this image in the albumInfo
 
200
@@ -1230,7 +1399,7 @@
 
201
             print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) . "\) Scaling $tmpThumbFile $msgPad";
 
202
             $cmd = "$scaleProg -scale x$opts{ty} -sharpen 5 \"$tmpFile\" \"$tmpThumbFile\"";
 
203
             $dieMsg = "\nCannot scale the thumbnail image $tmpThumbFile!\n";
 
204
-            system($cmd) == 0 or die $dieMsg;
 
205
+            mp_launch($cmd, $dieMsg);
 
206
 
 
207
             # increment the generated count
 
208
             $genCnt++;
 
209
@@ -1239,13 +1408,21 @@
 
210
             print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}} + 1) . "\) Skipping $tmpThumbFile $msgPad";
 
211
             $skipCnt++;
 
212
         }
 
213
+    }
 
214
 
 
215
+    # Wait for all processes to complete.
 
216
+    mp_wait_for_all_jobs();
 
217
+
 
218
+    # Second pass: now that all images have been converted,
 
219
+    # we can get all images informations.
 
220
+    for $i (0 .. $#{$albumInfo->{images}}) {
 
221
         # get the files size and X,Y info for the slide
 
222
         @tmpArr = &getImgInfo($albumInfo->{images}[$i]->{thumb});
 
223
         $albumInfo->{images}[$i]->{thumbkb} = $tmpArr[0];
 
224
         $albumInfo->{images}[$i]->{thumbx}  = $tmpArr[1];
 
225
         $albumInfo->{images}[$i]->{thumby}  = $tmpArr[2];
 
226
     }
 
227
+
 
228
     print "\r"; # move back to the start of the line
 
229
     print "$genCnt thumbnails generated. $msgPad\n" if $genCnt > 0;
 
230
     print "$skipCnt thumbnails skipped because they already existed.\n" if $skipCnt > 0;
 
231
@@ -1284,6 +1461,12 @@
 
232
     }
 
233
 
 
234
     # generate slide for each image
 
235
+    # First pass: scale all images. This is done in parallel
 
236
+    # if possible, with jobs launched in the background.
 
237
+    # Also, we remember if watermarking is necessary for the
 
238
+    # next pass.
 
239
+    my %may_need_watermarking;
 
240
+
 
241
     for $i (0 .. $#{$albumInfo->{images}}) {
 
242
         # get the name of the file and create the slide filename
 
243
         $tmpFile = $albumInfo->{images}[$i]->{file};
 
244
@@ -1304,45 +1487,67 @@
 
245
         if (($opts{fs} or (!(-e $tmpSlideFile))) && !($opts{uo})) {
 
246
             # scale the image to the slide size specs
 
247
             print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) . "\) Scaling $tmpSlideFile $msgPad";
 
248
+
 
249
             # only scale the slide if it's Y height is greater than
 
250
             # the value of the sy option.
 
251
             if ($albumInfo->{images}[$i]->{height} > $opts{sy}) {
 
252
                 $cmd = "$scaleProg -scale x$opts{sy} -sharpen 5 \"$tmpFile\" \"$tmpSlideFile\"";
 
253
-                $dieMsg = "\nCannot scale the slide image!\n";
 
254
-                system($cmd) == 0 or die $dieMsg;
 
255
+                $dieMsg = "\nCannot scale the slide image $tmpSlideFile!\n";
 
256
+                mp_launch($cmd, $dieMsg);
 
257
+
 
258
             } else {
 
259
                 $dieMsg = "Cannot copy \"$tmpFile\" to \"$tmpSlideFile\"\n";
 
260
                 copy $tmpFile,$tmpSlideFile or die "$dieMsg $!\n";
 
261
             }
 
262
 
 
263
-            # if we're watermarking the slides, do it now.
 
264
-            if ($opts{ws}) {
 
265
-                # check to make sure we have a valid watermark program
 
266
-                # and that the watermark image exists
 
267
-                if ($waterMarkProg eq "none") {
 
268
-                    print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) ."\) CANNOT Watermark $tmpSlideFile. No Watermark program found! $msgPad";
 
269
-                } else {
 
270
-                    print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) ."\) Watermarking $tmpSlideFile $msgPad";
 
271
-                    if ($waterMarkProg eq "composite") {
 
272
-                        $cmd = "$waterMarkProg -compose over -gravity $opts{wg} $opts{wf} \"$tmpSlideFile\" \"$tmpSlideFile\"";
 
273
-                    } else {
 
274
-                        # combine reversed the order the image and watermark
 
275
-                        # were listed on the command line.
 
276
-                        $cmd = "$waterMarkProg -compose over -gravity $opts{wg} \"$tmpSlideFile\" $opts{wf} \"$tmpSlideFile\"";
 
277
-                    }
 
278
-                    $dieMsg = "\nCannot watermark the slide image!\n";
 
279
-                    system($cmd) == 0 or die $dieMsg;
 
280
-                }
 
281
-            }
 
282
+            # Remember that this slide may need watermarking.
 
283
+            $may_need_watermarking{$i} = 1;
 
284
 
 
285
             # increment the generated count
 
286
             $genCnt++;
 
287
+
 
288
         } else {
 
289
             # increment the skipped count
 
290
             print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) . "\) Skipping $tmpSlideFile $msgPad";
 
291
             $skipCnt++;
 
292
         }
 
293
+    }
 
294
+
 
295
+    # Wait for all processes to complete.
 
296
+    mp_wait_for_all_jobs();
 
297
+
 
298
+    # Second pass: watermarking. We do this in parallel too.
 
299
+    # Note that we do this pass only if necessary.
 
300
+    if ($opts{ws}) {
 
301
+        for $i (0 .. $#{$albumInfo->{images}}) {
 
302
+            next if !exists $may_need_watermarking{$i};
 
303
+
 
304
+            # check to make sure we have a valid watermark program
 
305
+            # and that the watermark image exists
 
306
+            if ($waterMarkProg eq "none") {
 
307
+                print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) ."\) CANNOT Watermark $tmpSlideFile. No Watermark program found! $msgPad";
 
308
+
 
309
+            } else {
 
310
+                print "\r\(" . ($i+1) . "/" . ($#{$albumInfo->{images}}+1) ."\) Watermarking $tmpSlideFile $msgPad";
 
311
+                if ($waterMarkProg eq "composite") {
 
312
+                    $cmd = "$waterMarkProg -compose over -gravity $opts{wg} $opts{wf} \"$tmpSlideFile\" \"$tmpSlideFile\"";
 
313
+                } else {
 
314
+                    # combine reversed the order the image and watermark
 
315
+                    # were listed on the command line.
 
316
+                    $cmd = "$waterMarkProg -compose over -gravity $opts{wg} \"$tmpSlideFile\" $opts{wf} \"$tmpSlideFile\"";
 
317
+                }
 
318
+                $dieMsg = "\nCannot watermark the slide image $tmpSlideFile!\n";
 
319
+                mp_launch($cmd, $dieMsg);
 
320
+            }
 
321
+        }
 
322
 
 
323
+        # Wait for all processes to complete.
 
324
+        mp_wait_for_all_jobs();
 
325
+    }
 
326
+
 
327
+    # Third pass: now that all slide have been generated, we can get
 
328
+    # the images informations.
 
329
+    for $i (0 .. $#{$albumInfo->{images}}) {
 
330
         # get the image info for the slide
 
331
         if ($opts{uo}) {
 
332
             # we're using the originals and already have the