Sum values along a dimension - optimized version
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(array_type), | intent(in), | target | :: | a | ||
| integer, | intent(in) | :: | dim |
module function sum_array(a, dim) result(c) !! Sum values along a dimension - optimized version implicit none class(array_type), intent(in), target :: a integer, intent(in) :: dim type(array_type), pointer :: c integer :: s, i, n_rows, n_cols ! Cache dimensions n_rows = size(a%val, 1) n_cols = size(a%val, 2) if(dim.eq.1)then c => a%create_result(array_shape=[1, n_cols]) c%val(1,:) = sum(a%val, dim=1) else if(dim.eq.2)then c => a%create_result(array_shape=[a%shape, 1]) c%val(:,1) = sum(a%val, dim=2) c%is_sample_dependent = .false. else call stop_program("sum_array: only 1 or 2 dimensions are supported") end if c%indices = [dim, 1] c%get_partial_left => get_partial_sum !c%get_partial_right => get_partial_sum_forward c%get_partial_left_val => get_partial_sum_val c%get_partial_left_val_sum => get_partial_sum_val_sum if(a%requires_grad)then c%requires_grad = .true. c%is_forward = a%is_forward c%operation = 'sum_array' c%left_operand => a c%owns_left_operand = a%is_temporary end if end function sum_array