Accelerate my code for large array calculation.
Mostrar comentarios más antiguos
I'm dealing with a problem that I have to do nearly 700,000 loops and also I have to use ismembc and ismembc2 to find a 54-sized array in a very large array (~1e8).
Below is my code:
[Nofr_list,index_list,Vout_list] = deal(cell(Nr,1));
Pf_x=[1 1 1 1].*0.25;ind_x=[4 3 2 1];
Pf_y=[1 -1 -1 1].*0.25;ind_y=[4 3 2 1];
Pf_z=[-1 1 1 -1].*0.25;ind_z=[1 2 3 4];
first=[0 0 1 1];second=[1 0 1 0];
for Nofr = 1:Nr %iterate different active parents.
rb_matrix=bsxfun(@times, bitget(list_r_cardH.r_list(Nofr),NSites:-1:1), repelem(1,NHterms,1));
lind1=sub2ind(size(rb_matrix),1:54,[sites1_x,sites1_y,sites1_z]);
lind2=sub2ind(size(rb_matrix),1:54,[sites2_x,sites2_y,sites2_z]);
r=2*rb_matrix(lind1)-rb_matrix(lind2)+2;
pf=[Pf_x(r(1:18))';Pf_y(r(19:36))';Pf_z(r(37:54))'];
ind=[ind_x(r(1:18))';ind_y(r(19:36))';ind_z(r(37:54))'];
rb_matrix(lind1)=first(ind);
rb_matrix(lind2)=second(ind);
child_int=rb_matrix*(2.^(NSites-1:-1:0))';
p_list=list_parent_transf.parent(child_int+1);
mem=ismembc(p_list,list_r_cardH.r_list);
Loc=ismembc2(p_list,list_r_cardH.r_list);
g_list=list_parent_transf.transf(child_int(mem)+1);
pf=pf(mem);
chi_sum = Chi_r(g_list)+1i*Chi_i(g_list);
Vout=accumarray((Loc(Loc~=0))',(pf.*chi_sum).*(sqrt(abs(list_r_cardH.cardH_list(Loc(Loc~=0))./list_r_cardH.cardH_list(Nofr))))');
index_list{Nofr} = find(abs(Vout)>1e-6);
Nofr_list{Nofr} = repelem(Nofr,length(index_list{Nofr}),1);
Vout_list{Nofr} = Vout(index_list{Nofr});
end
Nofr_list=cell2mat(Nofr_list);
index_list=cell2mat(index_list);
Vout_list=cell2mat(Vout_list);
I try to vectorize my code.
Vout is a really sparse array and after calculating Vout I want to only save the non-zero terms.
The most time-consuming part is
mem=ismembc(p_list,list_r_cardH.r_list);
Loc=ismembc2(p_list,list_r_cardH.r_list);
and
Vout=accumarray((Loc(Loc~=0))',(pf.*chi_sum).*(sqrt(abs(list_r_cardH.cardH_list(Loc(Loc~=0))./list_r_cardH.cardH_list(Nofr))))');
where p_list is 54 sized-array and list_r_cardH.r_list has size ~1e8.(list_parent_transf.parent also has size 1e8 and list_r_cardH.r_list and list_r_cardH.cardH_list each has size ~7e6.) I load list_r_cardH and list_parent_transf from the mat file. Is there a way to accelerate my code?
Also, by searching the internet, I learned that using cell will generate lots of overheads which is time-consuming but when I tried to use array to save my result it didn't become faster.
Thanks!
5 comentarios
OCDER
el 5 de Jul. de 2018
You could avoid the 2nd ismembc. From this webpage: https://undocumentedmatlab.com/blog/ismembc-undocumented-helper-function,
It says "Whereas ismembc returns an array of logical values, ismembc2 returns the index locations of the found members."
mem=ismembc(p_list,list_r_cardH.r_list); %use this logical index.
Loc=ismembc2(p_list,list_r_cardH.r_list); %delete. same as find(mem)
With that said... I don't understand this then:
Loc(Loc~=0)
as that would be just be the same as Loc since Loc can't have a zero index.
Just curious, How did you determine ismembc is the slowest step? Can you upload the profile results?
Chi-Huei Lin
el 5 de Jul. de 2018
OCDER
el 5 de Jul. de 2018
Here are more tips:
Does the size of rb_matrix change every loop? If not, you could pull it out the loop.
I notice a lot of transpose operations. Although it's fast, doing it > 700K times can add up time. If possible, try not to transpose matrix every loop.
Save all constant vectors outside the loop, such as 1:54 and 19:36.
Can you show the profile results after say, 10000 iterations? It's hard to be accurate which is the rate limiting step at 1 iteration.
Chi-Huei Lin
el 5 de Jul. de 2018
Editada: Chi-Huei Lin
el 5 de Jul. de 2018
Chi-Huei Lin
el 5 de Jul. de 2018
Respuestas (1)
Steven Lord
el 5 de Jul. de 2018
Can you describe in a bit more detail exactly what you mean by "find a 54-sized array in a very large array (~1e8)"? When I read that description, I interpret it as you have a large vector of data and you want to locate a smaller vector inside that larger vector like finding the smaller vector 'the' inside the larger vector 'the quick brown fox jumped over the lazy dog' but with numbers instead of letters. Consider using strfind in this case.
Or do you have a cell array of character vectors and you want to find a smaller cell array inside them, like in the example below finding the contents of smallerCell inside largerCell?
smaller = 'the';
larger = 'the quick brown fox jumped over the lazy dog';
largerCell = strsplit(larger)
smallerCell = {'the', 'lazy'}
In this case, if the first element of smallerCell is "uncommon" in largerCell, you may be able to more quickly find that first element then compare a smaller number of potential match locations.
locationOfFirst = find(ismember(largerCell, smallerCell{1}))
for k = locationOfFirst
start = k;
endpoint = start + length(smallerCell)-1;
partToMatch = largerCell(start:endpoint);
if isequal(partToMatch, smallerCell)
fprintf('Found a match at starting location %d.\n', start);
end
end
If a different element in smallerCell is the uncommon one you could tweak the computation of locationOfFirst, start, and endpoint to find locations of that uncommon element first and compare the subsets in that vicinity.
If neither of those describe your use case, please share more details as they may allow us to offer better suggestions to achieve your goal more quickly.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

