rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
gram_schmidt_parallel.m
Go to the documentation of this file.
1 function Xon = gram_schmidt_parallel(X, K, epsilon, n_on)
2 % function Xon = gram_schmidt_parallel(X, k, epsilon, n_on)
3 % performs classical gram schmidt orthonormalization algorithm with
4 % re-orthogonalization. loss of orthogonality is a multiple of machine
5 % precision.
6 % - X: vectors to be orthonormalized
7 % - K: inner product matrix
8 % - epsilon: norm threshold
9 % - n_on: if given, first n_on vectors are assumed to be already orthonormal
10 
11 if nargin < 4
12  n_on = 0;
13 end
14 
15 pool = gcp;
16 
17 nworkers = pool.NumWorkers;
18 nvecs = size(X, 2);
19 ndofs = ceil(size(X, 1) / nworkers);
20 
21 X_p = Composite(nworkers);
22 K_p = Composite(nworkers);
23 
24 for i = 1:nworkers
25  X_p{i} = X(1+(i-1)*ndofs : min(i*ndofs, size(X, 1)), :);
26  K_p{i} = K(1+(i-1)*ndofs : min(i*ndofs, size(K, 1)), :);
27 end
28 
29 clear('X');
30 clear('K');
31 
32 spmd
33 
34  % storing matrix avoids multiple computation of inner products
35  KX_p = K_p * gcat(X_p, 1);
36 
37  for i = (n_on + 1):nvecs
38 
39  v_p = X_p(:, i);
40 
41  norm = sqrt(gplus(v_p' * KX_p(:, i)));
42 
43  niter = 0;
44 
45  % two orthogonalization iterations suffice
46  while norm > epsilon && niter < 2
47 
48  KXv = gplus(KX_p' * v_p);
49  KXv(n_on+1 : end) = 0;
50 
51  v_p = v_p - X_p * KXv;
52 
53  w = K_p * gcat(v_p, 1);
54 
55  norm = sqrt(gplus(v_p' * w));
56 
57  niter = niter + 1;
58 
59  end
60 
61  % include new vector
62  if norm > epsilon
63  n_on = n_on + 1;
64 
65  X_p(:, n_on) = v_p / norm;
66  KX_p(:, n_on) = w / norm;
67  end
68  end
69 end
70 
71 Xon = vertcat(X_p{:});
72 Xon = Xon(:, 1:n_on{1});
73 
74 end