|
On Sep 17, 9:42 am, "Sean "
<sean.dewol...@nospamplease.umit.maine.edu> wrote:
> Marios Karaoulis <marios.karaou...@gmail.com> wrote in message <b464d524-e1de-48ed-ac04-86dc06e1e...@x24g2000pro.googlegroups.com>...
> > On Sep 15, 4:57 pm, "Sean "
> > <sean.dewol...@nospamplease.umit.maine.edu> wrote:
> > > Marios Karaoulis <marios.karaou...@gmail.com> wrote in message <e194a14d-4ff3-46d5-892e-5d58581d5...@s24g2000pri.googlegroups.com>...
> > > > Hi, I have this problem
>
> > > > pa and pm are integer matrices, that I use as indexes to other
> > > > matrices
> > > > c is an 8X8 that is different in every loop
>
> > > > for i=1:100
> > > > for j=1:80
> > > > ia=pa(j); im=pm(j);
> > > > sum2=0;
> > > > for n=1:num_of_elements(i)
> > > > el=jtmp(n,i);
> > > > c=kernel(:,:,el);
> > > > tmp=cm.icon(1:8,el);
> > > > a1=pot(ia,tmp);
> > > > a2=pot(im,tmp);
> > > > S2=((conj(a1))'*a2).*c;
> > > > jam=sum(S2(:));
> > > > sum2=sum2+jam
> > > > end
> > > > jac(i,j)=sum2;
> > > > end
> > > > end
>
> > > > The first for loop is independent from the other's so I want to
> > > > transform it to
> > > > parfor i=1:100
>
> > > > but I can't store the value of sum2 to jac, because is is not valid
> > > > indexes are restricted.
>
> > > > How can i parallelize this?
>
> > > Give this a try:
> > > %%%%
> > > [ii, jj] = meshgrid(1:100,1:80); %come up with all indices ahead of time
> > > jac = zeros(100,80); %preallocate: listen to M-LINT!!!
> > > for idx = 1:numel(ii)
> > > ia=pa(jj(idx)); im=pm(jj(idx));
> > > sum2=0;
> > > jtmp_this_iter = jtmp(:,ii(idx));
> > > parfor n=1:num_of_elements(ii(idx))
> > > el=jtmp_this_iter(n);
> > > c=kernel(:,:,el); %I don't think there's a way to slice Kernel. At least not with the minimal amount of data you've given us. There may be a better way to run this whole thing if we had sample matrices.
> > > tmp=cm.icon(1:8,el);
> > > a1=pot(ia,tmp);
> > > a2=pot(im,tmp);
> > > S2=((conj(a1))'*a2).*c;
> > > jam=sum(S2(:));
> > > sum2=sum2+jam;
> > > end
> > > jac(ii(idx),jj(idx))=sum2;
> > > end
>
> > By the way, by using this without the parfor loop, reduced the
> > computation speed for almost 40%.
> > Cool.
>
> > But with the parfor loop, as you mentioned it, it takes for ever...
>
> First: Are the two results equal?
> Second: How many workers do you have open for parfor? If you only have one worker it'll be much slower. I would recommend closing your old workers before starting this job and then reopening them (just to be sure!). (matlabpool close force;matlabpool open N)
>
> I think you can avoid the inner loop entirely if you restructure (combination of reshape/replicate) your pot, jtmp, and (maybe) cm.icon to be 3-dimensional. Thus each slice corresponds to one element.
> You could then S2 and sum2 the same way.
Yes, results using the first algorithm, are the same. I haven't
succeeded with the second algorithm.
I have a core i7, and matlab sees 8 workers.
I will try again with your idea, but any help would be appreciate it.
Thanks.
P.S. By the way, can I send you the matrices?
|