473,396 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

using operators and large value types

I'm sorry if this is sounding like somewhat of a noob question. I'm
loading in a large binary array of 8x8 double precision floating point
matrices, right now this is defined something like

struct Mat8x8
{
double M11;
double M12;
Jul 31 '06 #1
9 1743
John <fo*@bar.comwrote:
I'm sorry if this is sounding like somewhat of a noob question. I'm
loading in a large binary array of 8x8 double precision floating point
matrices, right now this is defined something like

struct Mat8x8
{
double M11;
double M12;
.
.
.
double M86;
double M87;
double M88;

public static Mat8x8 operator*( Mat8x8 arg1, Mat8x8 arg2 )
{
...
}

};
The first question is to ask why it's a struct at all. Why not make it
a class?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 31 '06 #2
"John" <fo*@bar.comwrote in message
news:TK*******************@tornado.texas.rr.com...
I'm sorry if this is sounding like somewhat of a noob question. I'm
loading in a large binary array of 8x8 double precision floating point
matrices, right now this is defined something like

struct Mat8x8
{
double M11;
double M12;
.
.
.
double M86;
double M87;
double M88;

public static Mat8x8 operator*( Mat8x8 arg1, Mat8x8 arg2 )
{
...
}

};

Of course I have all these operators defined and a bunch of other methods
that do things to these matrices. The problem is that all of these things
are getting passed around by value. Often times the CPU is spending more
time copying the memory around than it is doing the calculations.

In C++ everything was getting passed around as a const reference, but of
course we can't do this in C#.

Any thoughts on the best way to go about solving this? I'd rather not
have to rewrite my functions to take "refs" or pointers to the value
types, because as far as I can tell I wouldn't be able to use the
operators anymore.
Change your Mat8x8 from a struct to a class, then it'll be passed by
reference, not by value - I'm not sure why you'd ever want a 512 byte object
to be treated as a value type.

-cd
Jul 31 '06 #3
Jon Skeet [C# MVP] wrote:
John <fo*@bar.comwrote:
>I'm sorry if this is sounding like somewhat of a noob question. I'm
loading in a large binary array of 8x8 double precision floating point
matrices, right now this is defined something like

struct Mat8x8
{
double M11;
double M12;
.
.
.
double M86;
double M87;
double M88;

public static Mat8x8 operator*( Mat8x8 arg1, Mat8x8 arg2 )
{
...
}

};

The first question is to ask why it's a struct at all. Why not make it
a class?
I figured (and was afraid) somebody would ask this. Because then I'd
have to new millions of these objects. Keeping them as a struct and
using an array allows me to blit the memory from their source. Also the
source data sometimes is passed as a pointer to a C function.

Basically I'm getting a "buffer of 8x8 matrices" and I need to do
operations on them. Sometimes I need to create a buffer of 8x8 matrices
and pass them by pointer to some non-CLR code using interop.

It's not really feasible to use them as a class.

Jul 31 '06 #4
Carl Daniel [VC++ MVP] wrote:
"John" <fo*@bar.comwrote in message
news:TK*******************@tornado.texas.rr.com...
>I'm sorry if this is sounding like somewhat of a noob question. I'm
loading in a large binary array of 8x8 double precision floating point
matrices, right now this is defined something like

struct Mat8x8
{
double M11;
double M12;
.
.
.
double M86;
double M87;
double M88;

public static Mat8x8 operator*( Mat8x8 arg1, Mat8x8 arg2 )
{
...
}

};

Of course I have all these operators defined and a bunch of other methods
that do things to these matrices. The problem is that all of these things
are getting passed around by value. Often times the CPU is spending more
time copying the memory around than it is doing the calculations.

In C++ everything was getting passed around as a const reference, but of
course we can't do this in C#.

Any thoughts on the best way to go about solving this? I'd rather not
have to rewrite my functions to take "refs" or pointers to the value
types, because as far as I can tell I wouldn't be able to use the
operators anymore.

Change your Mat8x8 from a struct to a class, then it'll be passed by
reference, not by value - I'm not sure why you'd ever want a 512 byte object
to be treated as a value type.

-cd

As I'm sure you know the math primitives in the Microsoft.DirectX
namespace are structs. I need them to be structs for the same reason
that those are structs (matrix buffers instead of vertex buffers) only
the problem is worse because my objects are far bigger. :-(

- john
Jul 31 '06 #5
"John" <fo*@bar.comwrote in message
news:l6*******************@tornado.texas.rr.com...
I figured (and was afraid) somebody would ask this. Because then I'd have
to new millions of these objects. Keeping them as a struct and using an
array allows me to blit the memory from their source. Also the source
data sometimes is passed as a pointer to a C function.

Basically I'm getting a "buffer of 8x8 matrices" and I need to do
operations on them. Sometimes I need to create a buffer of 8x8 matrices
and pass them by pointer to some non-CLR code using interop.

It's not really feasible to use them as a class.
Then I think your only option is to give up on operator overloading.

Instead of

public static Mat8x8 operator*( Mat8x8 arg1, Mat8x8 arg2 )

you'd use

public static Mat8x8 Multiply( ref Mat8x8 a1, ref Mat8x8 a2)

and you'd have to call it with explicit function call syntax, including the
ref operator:

Mat8x8 p = Mat8x8.Multiple(ref a1, ref a2)

then you'd avoid the copies.

-cd
Jul 31 '06 #6
Carl Daniel [VC++ MVP]
<cp*****************************@mvps.org.nospamwr ote:

<snip>
Change your Mat8x8 from a struct to a class, then it'll be passed by
reference, not by value - I'm not sure why you'd ever want a 512 byte object
to be treated as a value type.
It won't be passed by reference. The reference will be passed by value.
There's a big difference.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 31 '06 #7
John <fo*@bar.comwrote:
The first question is to ask why it's a struct at all. Why not make it
a class?

I figured (and was afraid) somebody would ask this. Because then I'd
have to new millions of these objects. Keeping them as a struct and
using an array allows me to blit the memory from their source. Also the
source data sometimes is passed as a pointer to a C function.

Basically I'm getting a "buffer of 8x8 matrices" and I need to do
operations on them. Sometimes I need to create a buffer of 8x8 matrices
and pass them by pointer to some non-CLR code using interop.

It's not really feasible to use them as a class.
If you want to do memory-efficient calculations, you could always make
them mutable. The semantics with operators should almost certainly be
to create new instances, but there's no reason why you shouldn't have
other methods which change the values.

Note that creating millions of objects really isn't a problem for .NET.
It's a lot nicer than passing 512 byte structs around...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jul 31 '06 #8
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Carl Daniel [VC++ MVP]
<cp*****************************@mvps.org.nospamwr ote:

<snip>
>Change your Mat8x8 from a struct to a class, then it'll be passed by
reference, not by value - I'm not sure why you'd ever want a 512 byte
object
to be treated as a value type.

It won't be passed by reference. The reference will be passed by value.
There's a big difference.
Whether there's a big difference depends on your expectations.
Mechanically, it's analogous to the difference in C++ between

void f(int* p) // 1

and

void f(int& p) // 2

Changing it to a ref type makes the default behavior equivalent to //1,
while leaving it a struct and adding the 'ref' keyword make the behavior
equivalent to //2.

Making it a reference type AND using the ref keyword is analogous to

void f(int*& p) //3

in C++, since you're now able to change both the reference and the value of
the referenced item.

The big difference (in changing from a value type to a ref type) is, as the
OP pointed out, the allocation of arrays and the ability to pass those
arrays to native code. An array of value type is a contiguous allocation,
while an array of reference type is a contiguous allocation of "pointers",
but the instances themselves are not guaranteed to be contiguous (nor are
they likely to even be coincidentally contiguous).

-cd
Jul 31 '06 #9
Carl Daniel [VC++ MVP]
<cp*****************************@mvps.org.nospamwr ote:
Change your Mat8x8 from a struct to a class, then it'll be passed by
reference, not by value - I'm not sure why you'd ever want a 512 byte
object
to be treated as a value type.
It won't be passed by reference. The reference will be passed by value.
There's a big difference.

Whether there's a big difference depends on your expectations.
Yes - it depends on whether your expectation is that when someone talks
about pass by reference semantics, they really mean it in the formal
sense. I do :)
Mechanically, it's analogous to the difference in C++ between

void f(int* p) // 1

and

void f(int& p) // 2

Changing it to a ref type makes the default behavior equivalent to //1,
while leaving it a struct and adding the 'ref' keyword make the behavior
equivalent to //2.
Absolutely. And // 1 involves passing a pointer by value, not passing
anything by reference. (For evidence of this, //1 would be available in
C, and the C spec explicitly states that C always uses pass-by-value
semantics.)
Making it a reference type AND using the ref keyword is analogous to

void f(int*& p) //3

in C++, since you're now able to change both the reference and the value of
the referenced item.
Yup - passing a pointer by reference.
The big difference (in changing from a value type to a ref type) is, as the
OP pointed out, the allocation of arrays and the ability to pass those
arrays to native code. An array of value type is a contiguous allocation,
while an array of reference type is a contiguous allocation of "pointers",
but the instances themselves are not guaranteed to be contiguous (nor are
they likely to even be coincidentally contiguous).
Yup, I agree with all of that. I just disagree with your claim that
making it a reference type would give pass by reference semantics.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 1 '06 #10

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

Similar topics

7
by: bberu | last post by:
Hi, I know it is possible to refine operators (like +, -, * or /) on classes. But is it possible on simple types ? ex : If I have a type like : typedef int vector is it possible to redefine a...
6
by: TB | last post by:
This is a repost of something I had posted four months ago with no response, I'm hoping to get some feedback... From the Microsoft .net Core Reference (pg. 104) C# shares the AND (short-circuit)...
7
by: Jim Bancroft | last post by:
Hi everyone, A basic one here, I think. I haven't found the pattern yet, but sometimes when I cast a variable to another type using the "C" style cast operator the compiler refuses to play...
52
by: mavic | last post by:
hi to everybody,i need help for my project....how to make a program that accept any integer and convert it binary,octal and hexadecimal.pls help me...thanks
3
by: Dean Roddey | last post by:
I just upgraded the compiler version we use in our product (we'd stuck with a very old one for a long time) and it seems that things have moved forward while we were off in the wilderness, and I'm...
28
by: dspfun | last post by:
I'm trying to get a good understanding of how unary operators work and have some questions about the following test snippets. int *p; ~!&*++p--; It doesn't compile, why? The problem seems to be...
18
by: Zach | last post by:
Can someone list the various macro operators and what they mean. Came across a function macro: #define max(a, b) ((a)>(b)?(a):(b)) What does "?" amd ":" mean in this statement? Zach
4
by: Travis | last post by:
So I have been doing C++ for quite awhile but have never had too many occasions to use |. What I'm trying to provide is ability to say enum myEnum = myEnum::one | myEnum::two. I'm not sure if...
6
by: Arjen | last post by:
Hi, Form a performance perspective, is it wise to use the ref statement as much as possible? Thanks! Arjen
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.