473,854 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Quick way to zero array

Man, that title should be in a poem, but anyways...So, I have this
program, and it has an array of 40 million elements. The problem is
that I have a for loop that continually is doing stuff to this array,
and for each iteration, I have to zero out the array. This is how I am
currently doing it: // Zero out the lnscores for( count=0; count <
chunksize; count++ ) lnscores[count] = 0; Is there no quicker way
to do this? I know there must be a trick since this array is one big
contiguous chunk of memory right?

May 12 '06
14 52487
> BTW, 40 million elements array, are u doing image processing or some > other kind of signal processing? Close. I'm doing some quadratic field sieve work. The program is actually quite fast. I factored a 203-bit number in about 9 hours on a 2.8GHz P4. >> Why do you have to zero the array? It has to do with keeping a score (hence the name "lnscore") of the residues modulo n. I process 40 million each go-round and then look at the scores to see if they are high-enough to progress through the algorithm. I usually get 1,000 that pass out of 40 million residues which are then narrowed down to around 10 numbers that I am actually looking for. It will find these 10 in around 11 seconds. Granted, I actually have to find tens of thousands of these before I can factor anything. >> I suggest you >> seek a better algorithm--for example, whenever you update an element >> in an iteration, before you leave that loop, zero that element, so >> the whole array returns to the zeroed state. No, this will not work. I have thought about it, and there is just no way to do it. Like I said, my program spits out the scores into lnscores (this is actually the "sieve" part of the QFS...it's pretty fast), and then I must search through the scores to take out the best ones. Only then can I return everything to zero. Oh, and I tried using memset(), but it was not any faster than the loop I had, and it hung after one iteration. So, still looping for now. Thanks for all the replies. I didn't expect so many :)

May 12 '06 #11

<ro**********@g mail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
BTW, 40 million elements array, are u doing image processing or some other kind of signal processing?


<corrected header...> "Julian V. Noble" <jv*@virginia.e du> wrote in message news:e4******** **@murdoch.acc. Virginia.EDU...
I suggest you
seek a better algorithm--for example, whenever you update an element
in an iteration, before you leave that loop, zero that element, so
the whole array returns to the zeroed state.

No, this will not work. I have thought about it, and there is just no way
to do it. Like I said, my program spits out the scores into lnscores
(this is actually the "sieve" part of the QFS...it's pretty fast), and then I must search through the scores to take out the best ones. Only then
can I return everything to zero. Oh, and I tried using memset(), but
it was not any faster than the loop I had, and it hung after one iteration. So, still looping for now. Thanks for all the replies. I didn't expect so many :)

In regards to:
I must search through the scores to take out the best ones.


You didn't read Keith's post. A simple approach (I'll mention a better one
further down) is to use a second array of unsigned char. As you assign a
value to elements in array lnscores[], set an "in use" indicator in the
second array. Then you only need to clear and search through the in_use
array to find valid lnscores[]. Let's say your array is 40 instead of 40
million:

unsigned long lnscores[40];
unsigned char in_use[40];

memset(in_use,0 ,40); /* clear in use array */

/* in loop finding potential residues */
lnscores[index]=somevalue;
in_use[index]=1; /* all non-used in_use values are 0 */

/* in loop searching for potential residues */
if (in_use[index]) /* if not in use skip */
{
/* look at lnscores[index] */
}

This will reduce clearing 40 million large values to 40 million small
values.

Now that you (hopefully) understood that, a better approach is to hash
function or stack to just store the indexes of values you modified. This
would reduce the number of values you need to clear to the peak number of
residues.
Rod Pemberton
May 12 '06 #12
aha
there are more efficient methods
u know TAOCP, by Knuth
there is a mazing method as this:

suppose the array u will use is A[N], where N maybe very large, such as 40
Million
then u just need another two array, such as B[N] & C[N], all of them are of
integer type

#define N (40 * 1024 * 1024)
int A[N], B[N], C[N];
int cnt = 0;

//u just only need to reset the counter *cnt*

//when u need to refer an element such as A[i]
//u first check
int t = B[i];
if(t >= 0 && t < N && C[t] == i) {
//that's ok, this element has been accessed before, so it must be goood
}else{
//this element has not been initalized
A[i] = 0;
C[cnt] = i;
B[i] = cnt++;
}

//so, this is really maybe the best, *when u need, u do that!*

"Rod Pemberton" <do*********@bi tfoad.cmm> wrote in message
news:e4******** **@defiant.vmun ix.org...

<ro**********@g mail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
BTW, 40 million elements array, are u doing image processing or some other kind of signal processing?


<corrected header...>
"Julian V. Noble" <jv*@virginia.e du> wrote in message

news:e4******** **@murdoch.acc. Virginia.EDU...
I suggest you
seek a better algorithm--for example, whenever you update an element
in an iteration, before you leave that loop, zero that element, so
the whole array returns to the zeroed state.
No, this will not work. I have thought about it, and there is just no

way to do it. Like I said, my program spits out the scores into lnscores
(this is actually the "sieve" part of the QFS...it's pretty fast), and

then
I must search through the scores to take out the best ones. Only then
can I return everything to zero. Oh, and I tried using memset(), but
it was not any faster than the loop I had, and it hung after one

iteration.
So, still looping for now. Thanks for all the replies. I didn't expect

so many :)

In regards to:
I must search through the scores to take out the best ones.
You didn't read Keith's post. A simple approach (I'll mention a better

one further down) is to use a second array of unsigned char. As you assign a
value to elements in array lnscores[], set an "in use" indicator in the
second array. Then you only need to clear and search through the in_use
array to find valid lnscores[]. Let's say your array is 40 instead of 40
million:

unsigned long lnscores[40];
unsigned char in_use[40];

memset(in_use,0 ,40); /* clear in use array */

/* in loop finding potential residues */
lnscores[index]=somevalue;
in_use[index]=1; /* all non-used in_use values are 0 */

/* in loop searching for potential residues */
if (in_use[index]) /* if not in use skip */
{
/* look at lnscores[index] */
}

This will reduce clearing 40 million large values to 40 million small
values.

Now that you (hopefully) understood that, a better approach is to hash
function or stack to just store the indexes of values you modified. This
would reduce the number of values you need to clear to the peak number of
residues.
Rod Pemberton

May 13 '06 #13

"daizisheng " <da********@gma il.com> wrote in message
news:e4******** **@news.cn99.co m...
aha
there are more efficient methods
u know TAOCP, by Knuth
there is a mazing method as this:

suppose the array u will use is A[N], where N maybe very large, such as 40
Million
then u just need another two array, such as B[N] & C[N], all of them are of integer type

#define N (40 * 1024 * 1024)
int A[N], B[N], C[N];
int cnt = 0;

//u just only need to reset the counter *cnt*

//when u need to refer an element such as A[i]
//u first check
int t = B[i];
if(t >= 0 && t < N && C[t] == i) { sorry for a mistake
the condition is
if(t >= 0 && t < cnt && C[t] == i) //that's ok, this element has been accessed before, so it must be goood
}else{
//this element has not been initalized
A[i] = 0;
C[cnt] = i;
B[i] = cnt++;
}

//so, this is really maybe the best, *when u need, u do that!*

"Rod Pemberton" <do*********@bi tfoad.cmm> wrote in message
news:e4******** **@defiant.vmun ix.org...

<ro**********@g mail.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
> BTW, 40 million elements array, are u doing image processing or some
other kind of signal processing?


<corrected header...>
"Julian V. Noble" <jv*@virginia.e du> wrote in message

news:e4******** **@murdoch.acc. Virginia.EDU...
> I suggest you
> seek a better algorithm--for example, whenever you update an element
> in an iteration, before you leave that loop, zero that element, so
> the whole array returns to the zeroed state.

No, this will not work. I have thought about it, and there is just no way to do it. Like I said, my program spits out the scores into lnscores
(this is actually the "sieve" part of the QFS...it's pretty fast), and

then
I must search through the scores to take out the best ones. Only then
can I return everything to zero. Oh, and I tried using memset(), but
it was not any faster than the loop I had, and it hung after one

iteration.
So, still looping for now. Thanks for all the replies. I didn't expect so
many :)

In regards to:
I must search through the scores to take out the best ones.


You didn't read Keith's post. A simple approach (I'll mention a better

one
further down) is to use a second array of unsigned char. As you assign

a value to elements in array lnscores[], set an "in use" indicator in the
second array. Then you only need to clear and search through the in_use
array to find valid lnscores[]. Let's say your array is 40 instead of 40 million:

unsigned long lnscores[40];
unsigned char in_use[40];

memset(in_use,0 ,40); /* clear in use array */

/* in loop finding potential residues */
lnscores[index]=somevalue;
in_use[index]=1; /* all non-used in_use values are 0 */

/* in loop searching for potential residues */
if (in_use[index]) /* if not in use skip */
{
/* look at lnscores[index] */
}

This will reduce clearing 40 million large values to 40 million small
values.

Now that you (hopefully) understood that, a better approach is to hash
function or stack to just store the indexes of values you modified. This would reduce the number of values you need to clear to the peak number of residues.
Rod Pemberton


May 13 '06 #14
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote
I mostly use bzero for such things.


Why choose the non-standard bzero when there is a perfectly standard
memset?

If memset isn't fast enough, probably the only way is to go for a
non-standard function.

Actually bzero is probably just a slightly different memset(), but many
machines have hardware for clearing memory buffers in parallel. There's no
easy way to give an ANSI C interface to this sort of operation, but normally
you would set the operation going, do something else, and then check a flag
to see if it had completed.

The other solution is to use an algorithm that doesn't require the array to
be initialised.
--
www.personal.leeds.ac.uk/~bgy1mm


May 13 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
8350
by: Berk Birand | last post by:
Hi, I am just learning about the array/pointer duality in C/C++. I couldn't help wondering, is there a way to pass an array by value? It seems like the only way to do is to pass it by reference?? Thanks, BB
13
8750
by: DazedAndConfused | last post by:
Is there a quick way to fill an array with zeros or make all elements empty/nothing?
22
2309
by: spam.noam | last post by:
Hello, I discovered that I needed a small change to the Python grammar. I would like to hear what you think about it. In two lines: Currently, the expression "x" is a syntax error. I suggest that it will be evaluated like "x", just as "x" is evaluated like "x" right now.
3
1732
by: Brian | last post by:
A quick question here - What can be achieved in IL which is not possible in C# ? o Creation of an ArrayList o Creation of a Dictionary o Creation of a two dimensional array o Creation of a non-zero based array Please let me know what is the right answer. Many thanks.
0
367
by: Adam Warner | last post by:
Hi all, One cannot return a pointer to an array in C since there are no first class array types. But one can return a pointer to an incomplete array via the illegal but widely supported zero array struct hack: #include <stdlib.h> typedef struct byte_vector_t byte_vector_t;
10
2407
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Got a simple question. I am new to c# but this is not making me any sence. If i declare: string myStringArray = new string; How the heck could i fill it with more than one element? Regards
12
3987
by: rajus | last post by:
I want to store the (x,y) coordinates of about 10,000 points in a 2D array.How can I store them and retrieve them later?
9
1495
by: John B | last post by:
Hi all, Say I have the int 123456789 What would be the quickest/best way to convert it to int{1,2,3,4,5,6,7,8,9} What I came up with was to determine the largest factor of 10 (100M) divide by that, truncate decimals and that'd be the 1st number, subtract that number multiplied by the current factor, next smallest factor etc.. But it seems very roundabout, surely there'd be a better way :)
24
2206
by: DomoChan | last post by:
the code below will compile in visual c++ 2003, but im not sure its valid. unsigned char myString = ""; after this line executes, all the bytes within myString are indeed set to '0's' but is this really valid c++ or c? where can I find out how this is implemented? Im concerned because I had a 3rd party library wrapper which was
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11031
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10685
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10763
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7082
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5750
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.