@@ -330,12 +330,17 @@ contains
330330 ! data accessors
331331 !==================================================================
332332
333+ logical(c_bool) elemental function skip(sym,row,col)
334+ integer(ilp), intent(in) :: sym, row, col
335+ skip = (sym == sparse_lower .and. row < col) .or. (sym == sparse_upper .and. row > col)
336+ end function
337+
333338 #:for k1, t1, s1 in (KINDS_TYPES)
334339 pure ${t1}$ function at_value_coo_${s1}$(self,ik,jk) result(val)
335340 class(COO_${s1}$_type), intent(in) :: self
336341 integer(ilp), intent(in) :: ik, jk
337342 integer(ilp) :: k, ik_, jk_
338- logical :: transpose
343+ logical(c_bool) :: transpose
339344 ! naive implementation
340345 if( (ik<1 .or. ik>self%nrows) .or. (jk<1 .or. jk>self%ncols) ) then
341346 val = ieee_value( 0._${k1}$ , ieee_quiet_nan)
@@ -373,14 +378,18 @@ contains
373378 class(COO_${s1}$_type), intent(inout) :: self
374379 ${t1}$, intent(in) :: val(:,:)
375380 integer(ilp), intent(in) :: ik(:), jk(:)
376- integer(ilp) :: k, i, j
381+ integer(ilp) :: k, i, j, row, col
377382 ! naive implementation
378383 do k = 1, self%nnz
379384 do i = 1, size(ik)
380- if( ik(i) /= self%index(1,k) ) cycle
385+ row = ik(i)
386+ if( row /= self%index(1,k) ) cycle
381387 do j = 1, size(jk)
382- if( jk(j) /= self%index(2,k) ) cycle
388+ col = jk(j)
389+ if( skip(self%storage,row,col) ) cycle
390+ if( col /= self%index(2,k) ) cycle
383391 self%data(k) = self%data(k) + val(i,j)
392+ exit
384393 end do
385394 end do
386395 end do
@@ -393,7 +402,7 @@ contains
393402 class(CSR_${s1}$_type), intent(in) :: self
394403 integer(ilp), intent(in) :: ik, jk
395404 integer(ilp) :: k, ik_, jk_
396- logical :: transpose
405+ logical(c_bool) :: transpose
397406 ! naive implementation
398407 if( (ik<1 .or. ik>self%nrows) .or. (jk<1 .or. jk>self%ncols) ) then
399408 val = ieee_value( 0._${k1}$ , ieee_quiet_nan)
@@ -431,13 +440,17 @@ contains
431440 class(CSR_${s1}$_type), intent(inout) :: self
432441 ${t1}$, intent(in) :: val(:,:)
433442 integer(ilp), intent(in) :: ik(:), jk(:)
434- integer(ilp) :: k, i, j
443+ integer(ilp) :: k, i, j, row, col
435444 ! naive implementation
436445 do i = 1, size(ik)
437- do k = self%rowptr(ik(i)), self%rowptr(ik(i)+1)-1
446+ row = ik(i)
447+ do k = self%rowptr(row), self%rowptr(row+1)-1
438448 do j = 1, size(jk)
439- if( jk(j) == self%col(k) ) then
449+ col = jk(j)
450+ if( skip(self%storage,row,col) ) cycle
451+ if( col == self%col(k) ) then
440452 self%data(k) = self%data(k) + val(i,j)
453+ exit
441454 end if
442455 end do
443456 end do
@@ -451,7 +464,7 @@ contains
451464 class(CSC_${s1}$_type), intent(in) :: self
452465 integer(ilp), intent(in) :: ik, jk
453466 integer(ilp) :: k, ik_, jk_
454- logical :: transpose
467+ logical(c_bool) :: transpose
455468 ! naive implementation
456469 if( (ik<1 .or. ik>self%nrows) .or. (jk<1 .or. jk>self%ncols) ) then
457470 val = ieee_value( 0._${k1}$ , ieee_quiet_nan)
@@ -489,13 +502,17 @@ contains
489502 class(CSC_${s1}$_type), intent(inout) :: self
490503 ${t1}$, intent(in) :: val(:,:)
491504 integer(ilp), intent(in) :: ik(:), jk(:)
492- integer(ilp) :: k, i, j
505+ integer(ilp) :: k, i, j, row, col
493506 ! naive implementation
494507 do j = 1, size(jk)
495- do k = self%colptr(jk(j)), self%colptr(jk(j)+1)-1
508+ col = jk(j)
509+ do k = self%colptr(col), self%colptr(col+1)-1
496510 do i = 1, size(ik)
497- if( ik(i) == self%row(k) ) then
511+ row = ik(i)
512+ if( skip(self%storage,row,col) ) cycle
513+ if( row == self%row(k) ) then
498514 self%data(k) = self%data(k) + val(i,j)
515+ exit
499516 end if
500517 end do
501518 end do
@@ -509,7 +526,7 @@ contains
509526 class(ELL_${s1}$_type), intent(in) :: self
510527 integer(ilp), intent(in) :: ik, jk
511528 integer(ilp) :: k, ik_, jk_
512- logical :: transpose
529+ logical(c_bool) :: transpose
513530 ! naive implementation
514531 if( (ik<1 .or. ik>self%nrows) .or. (jk<1 .or. jk>self%ncols) ) then
515532 val = ieee_value( 0._${k1}$ , ieee_quiet_nan)
@@ -547,13 +564,17 @@ contains
547564 class(ELL_${s1}$_type), intent(inout) :: self
548565 ${t1}$, intent(in) :: val(:,:)
549566 integer(ilp), intent(in) :: ik(:), jk(:)
550- integer(ilp) :: k, i, j
567+ integer(ilp) :: k, i, j, row, col
551568 ! naive implementation
552569 do k = 1 , self%K
553570 do j = 1, size(jk)
571+ col = jk(j)
554572 do i = 1, size(ik)
555- if( jk(j) == self%index(ik(i),k) ) then
556- self%data(ik(i),k) = self%data(ik(i),k) + val(i,j)
573+ row = ik(i)
574+ if( skip(self%storage,row,col) ) cycle
575+ if( col == self%index(row,k) ) then
576+ self%data(row,k) = self%data(row,k) + val(i,j)
577+ exit
557578 end if
558579 end do
559580 end do
@@ -567,7 +588,7 @@ contains
567588 class(SELLC_${s1}$_type), intent(in) :: self
568589 integer(ilp), intent(in) :: ik, jk
569590 integer(ilp) :: k, ik_, jk_, idx
570- logical :: transpose
591+ logical(c_bool) :: transpose
571592 ! naive implementation
572593 if( (ik<1 .or. ik>self%nrows) .or. (jk<1 .or. jk>self%ncols) ) then
573594 val = ieee_value( 0._${k1}$ , ieee_quiet_nan)
@@ -608,14 +629,18 @@ contains
608629 class(SELLC_${s1}$_type), intent(inout) :: self
609630 ${t1}$, intent(in) :: val(:,:)
610631 integer(ilp), intent(in) :: ik(:), jk(:)
611- integer(ilp) :: k, i, j, idx
632+ integer(ilp) :: k, i, j, idx, row, col
612633 ! naive implementation
613634 do k = 1 , self%chunk_size
614635 do j = 1, size(jk)
636+ col = jk(j)
615637 do i = 1, size(ik)
616- idx = self%rowptr((ik(i) - 1)/self%chunk_size + 1)
617- if( jk(j) == self%col(k,idx) ) then
638+ row = ik(i)
639+ idx = self%rowptr((row - 1)/self%chunk_size + 1)
640+ if( skip(self%storage,row,col) ) cycle
641+ if( col == self%col(k,idx) ) then
618642 self%data(k,idx) = self%data(k,idx) + val(i,j)
643+ exit
619644 end if
620645 end do
621646 end do
0 commit comments