Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Pointers question

0 views
Skip to first unread message

tomg...@hotmail.com

unread,
May 15, 2008, 10:32:49 AM5/15/08
to
This is a follow on from an earlier post of mine regarding sorting.

I've never used pointers before and I'm finding the lingo a bit heavy.

Here's my problem.

I have a potentially very large matrix that looks like

1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

For my largest problem the matrix will have 11 columns and about
10,000,000 rows.

I'm only interested in the section that is nonzero but am not aware of
how to change the matrix size quickly and efficiently as more rows are
needed. I was told that pointers maybe the way to go. The example I
was shown was to declare a single column vector which represents my
first column i.e.

1
2
3
0
0
0
0

and then use a pointer that is 11 columns wide but grows to have the
same nunmber of rows as nonzero elements in the vector.

I'm not too sure how this can be done. If anyone has any ideas or
sample code that would be great.

Thanks

glen herrmannsfeldt

unread,
May 15, 2008, 1:45:25 PM5/15/08
to
tomg...@hotmail.com wrote:
(snip)

> I've never used pointers before and I'm finding the lingo a bit heavy.

> I have a potentially very large matrix that looks like

> 1 2 3 4 5 6
> 2 3 4 5 6 7
> 3 4 5 6 7 8
> 0 0 0 0 0 0
> 0 0 0 0 0 0
> 0 0 0 0 0 0
> 0 0 0 0 0 0

> For my largest problem the matrix will have 11 columns and about
> 10,000,000 rows.

> I'm only interested in the section that is nonzero but am not aware of
> how to change the matrix size quickly and efficiently as more rows are
> needed. I was told that pointers maybe the way to go. The example I
> was shown was to declare a single column vector which represents my
> first column i.e.

(snip)

> and then use a pointer that is 11 columns wide but grows to have the
> same nunmber of rows as nonzero elements in the vector.

In Fortran you can't have arrays of pointers. You can have
arrays of structures containing pointers, which is usually
close enough.

From your example, do you want to start with three rows,
and slowly increase to 11?

Here is a small program with an allocatable array of structures
containing pointers.

! declare a structure (derived type) containing an array pointer
type pr
real, pointer :: pr(:)
end type pr

! declare an allocatable array of the above structures.
type(pr), allocatable:: array(:)

! allocate 11 structures containing pointers
allocate(array(11))

do i=1,3
! allocate a row such that one of the pointers points to it.
allocate(array(i)%pr(1000000))
! initialize one of the elements
array(i)%pr(1)=i
enddo

print *,array(3)%pr(1)

end

Note that this sample uses an allocatable array of (structures
containing) pointers. Either could be done with either pointers
or allocatables in this case.

-- glen

tomg...@hotmail.com

unread,
May 15, 2008, 1:56:41 PM5/15/08
to
Thanks for the help.

I'm going to have a play around with the way I'm storing my data.

Cheers

Ron Ford

unread,
May 15, 2008, 7:18:30 PM5/15/08
to

Don't miss James van Buskirk's elsethread post regarding a very similar
matter. The subject is "Sorting Arrays." His solution works right out of
the box on gfortran 4.3.0.

You won't have arrays of pointers in fortran as you did with C. I think
what you do instead is have a module for them:
module row_ptr_mod
implicit none
private
public row_ptr
type row_ptr
integer, pointer :: ptr(:)
end type row_ptr
public operator(<)
interface operator(<)
module procedure less_than
end interface operator(<)
contains
function less_than(x,y)
type(row_ptr), intent(in) :: x, y
logical less_than
integer i

if(.NOT.associated(x%ptr) .OR. .NOT.associated(y%ptr)) then
write(*,*) 'Error in less than: disassociated pointer'
stop
end if
if(size(x%ptr) /= size(y%ptr)) then
write(*,*) 'Error in less than: unequal row lengths'
end if
do i = 1, size(x%ptr)
if(x%ptr(i) < y%ptr(i)) then
less_than = .TRUE.
return
else if(x%ptr(i) > y%ptr(i)) then
less_than = .FALSE.
return
end if
end do
less_than = .FALSE.
return
end function less_than
end module row_ptr_mod

It's a mantra around here that C pointers aren't fortran pointers.

If the rub is that the bubblesort he uses won't do well with ten thousand
balls to juggle, then I think you take this start and rewrite the sort to
suit. I would love to see you do that, since I don't have much experience
with such matters in fortran.

Good luck.
--
Ron Ford

alex

unread,
May 15, 2008, 10:20:14 PM5/15/08
to
On May 16, 7:18 am, Ron Ford <r...@nowhere.net> wrote:

> On Thu, 15 May 2008 10:56:41 -0700 (PDT), tomgu...@hotmail.com wrote:
> > Thanks for the help.
>
> > I'm going to have a play around with the way I'm storing my data.
>
> > Cheers
>
> Don't miss James van Buskirk's elsethread post regarding a very similar
> matter. The subject is "Sorting Arrays." His solution works right out of
> the box on gfortran 4.3.0.
>
> You won't have arrays of pointers in fortran as you did with C. I think
> what you do instead is have a module for them:
> module row_ptr_mod
> implicit none
> private
> public row_ptr
> type row_ptr
> integer, pointer :: ptr(:)
> end type row_ptr

it would be better, if we initialized the pointer PTR with null() in
user-defined type ROW_PTR.

type row_ptr
integer, pointer :: ptr(:) => null()
end type row_ptr

Michel Olagnon

unread,
May 16, 2008, 3:58:20 AM5/16/08
to

I think that you should transpose your matrix.

0 new messages