|
|
Hi.
I am currently learning Linq and have some trouble with the Linq Contains function.
Here is what I first tried:
DataClasses1DataContext con = new DataClasses1DataContext(@"myconstring");
bool contains = con.Images.Contains(con.Images.First(n => n.imageID == 2));
I was pretty shure this would work but contains became false.
I then tried this:
Image myImg = con.Images.First(n => n.imageID == 2);
Image myImg3 = con.Images.First(n => n.imageID == 2);
if (myImg.Equals(myImg3))
Console.WriteLine("hurra");
But this did not work either. The two instances did not equal eachother.
I continued to try with this:
Image myImg2 = new Image
{
created = DateTime.Parse("23.05.2008 00:00:00"),
description = "Jalla",
imageID = 2,
name = "test",
searchWords = "halla",
systemID = 1
};
Image myImg4 = new Image
{
created = DateTime.Parse("23.05.2008 00:00:00"),
description = "Jalla",
imageID = 2,
name = "test",
searchWords = "halla",
systemID = 1
};
if (myImg2.Equals(myImg4))
Console.WriteLine("hurra");
But still they did not equal eachother. Only way I got this to work was by adding my img instances to a Array of Images[] and then do contains on this.
Image[] imgs = {myImg, myImg2, myImg3, myImg4};
bool contains = imgs.Contains(myImg2);
This gives contains a true value. So then I tought that this should work:
bool contains = con.Images.ToList().Contains(con.Images.First(n => n.imageID == 2));
But still contains is set to false.
So I am stumped. Anyone got anny sugestions or hints to why this does not work?
|
|
|
|
|
I found one workaround, but I still wonder if there is a bether way of doing this. Here is a solution that works:
bool contains = con.Images.ToList().Contains(con.Images.First(n => n.imageID == 2),new checker());
and the checker class:
public class checker : IEqualityComparer<image>
{
#region IEqualityComparer<image> Members
public bool Equals(Image x, Image y)
{
if (x.imageID == y.imageID)
return true;
else
{
return false;
}
}
public int GetHashCode(Image obj)
{
return obj.GetHashCode();
}
#endregion
}</image></image>
And I am still verry curious to why this dont work:
Image myImg2 = new Image
{
created = DateTime.Parse("23.05.2008 00:00:00"),
description = "Jalla",
imageID = 2,
name = "test",
searchWords = "halla",
systemID = 1
};
Image myImg4 = new Image
{
created = DateTime.Parse("23.05.2008 00:00:00"),
description = "Jalla",
imageID = 2,
name = "test",
searchWords = "halla",
systemID = 1
};
if (myImg2.Equals(myImg4))
Console.WriteLine("hurra");
I see no reason why these two instances should not equal eachother.
Here is the autogenerated Image class if anyone wants to know how that looks:
[Table(Name="dbo.Images")]
public partial class Image
{
private int _imageID;
private int _systemID;
private string _name;
private string _description;
private string _searchWords;
private System.DateTime _created;
private System.Nullable<system.datetime> _uppdated;
public Image()
{
}
[Column(Storage="_imageID", AutoSync=AutoSync.Always, DbType="Int NOT NULL IDENTITY", IsDbGenerated=true)]
public int imageID
{
get
{
return this._imageID;
}
set
{
if ((this._imageID != value))
{
this._imageID = value;
}
}
}
[Column(Storage="_systemID", DbType="Int NOT NULL")]
public int systemID
{
get
{
return this._systemID;
}
set
{
if ((this._systemID != value))
{
this._systemID = value;
}
}
}
[Column(Storage="_name", DbType="NVarChar(50) NOT NULL", CanBeNull=false)]
public string name
{
get
{
return this._name;
}
set
{
if ((this._name != value))
{
this._name = value;
}
}
}
[Column(Storage="_description", DbType="NVarChar(2000)")]
public string description
{
get
{
return this._description;
}
set
{
if ((this._description != value))
{
this._description = value;
}
}
}
[Column(Storage="_searchWords", DbType="NVarChar(2000)")]
public string searchWords
{
get
{
return this._searchWords;
}
set
{
if ((this._searchWords != value))
{
this._searchWords = value;
}
}
}
[Column(Storage="_created", DbType="DateTime NOT NULL")]
public System.DateTime created
{
get
{
return this._created;
}
set
{
if ((this._created != value))
{
this._created = value;
}
}
}
[Column(Storage="_uppdated", DbType="DateTime")]
public System.Nullable<system.datetime> uppdated
{
get
{
return this._uppdated;
}
set
{
if ((this._uppdated != value))
{
this._uppdated = value;
}
}
}
}</system.datetime></system.datetime>
|
|
|
|
|
Continued from "Another" one automation error...[^]
Ok, now, we going some steps forward...
System architecture is as follows: there are number of out-of-process COM servers. Each one uses an in-process server to access .NET interop. So:
[out-of-process COM server No.1] --\
[out-of-process COM server No.2] -- \
........................................................... --> [in-process COM component] --> [.NET interop component]
[out-of-process COM server No.n] ---/
This architecture required because it get possible to set each application with its own configuration provided to .NET component.
From previous attempts we know, that .NET component with satellite assemblies should be placed in working directory of COM application. In my case I need to duplicate them in each COM application. Or to install them to GAC.
So, I have placed all assemblies (including resources) to GAC and... right, I got automation error...
Fuslogvw does not point to some error. All assemblies were found and loaded from GAC successfully (except en-culture resource file that really does not present and embedded - it should not be a case, does not it?)
So the questions are:
- Is there some way to optimize present architecture scheme?
- Why GAC installation does not work?
- Does COM application need some additional permissions to access GAC?
Thanks to all.
modified on Wednesday, January 7, 2009 4:13 AM
|
|
|
|
|
Anyone having idea for pdf to word/doc conversion !!!!!
Don't walk as if u rule the world,walk as if u don't care who tha Hell rules the world ...
|
|
|
|
|
r u looking in c# or any language
|
|
|
|
|
any language , C# preffered
Thanks
Don't walk as if u rule the world,walk as if u don't care who tha Hell rules the world ...
|
|
|
|
|
Any help ??????
Don't walk as if u rule the world,walk as if u don't care who tha Hell rules the world ...
|
|
|
|
|
|
No I am not looking for a tool but source code/dll/or some basic source from where i can get help that how can we read a pdf file in c# and convert that in doc/word file with images preserved...
Thanks
Don't walk as if u rule the world,walk as if u don't care who tha Hell rules the world ...
|
|
|
|
|
|
I have seen this earlier, this is not having any functionality to convert pdf to doc. Its creating,merging,splitting pdf but doesn't converting...
Thanks..
Don't walk as if u rule the world,walk as if u don't care who tha Hell rules the world ...
|
|
|
|
|
No problem. Sorry I could help more, but hopefully someone else comes up with an idea.
|
|
|
|
|
I've got a multi-threaded implementation of a lockless queue in C# using Interlocked.CompareExchange() and a multi-threaded implementation of a locked queue that uses lock() when it comes to enqueueing and dequeueing items. Essentially, what happens is that I use System.Threading.ThreadPool.QueueUserWorkItem() to queue 5-10 pieces of work (some function), which enqueues and dequeues from the lockless/locked queue several hundred thousand times.
A lot of articles have explained that using a lockless queue can improve performance when it comes to accessing a data structure, but I'm finding that the locked queue (which is simpler) ends up being faster. From a theoretical standpoint, I understand that synchronization primitives may be expensive, which is part of the reason why lockless should be faster. On the other hand, a lockless implementation with 5 operations working at the same time works by having one operation try and succeed, while for the other operations, they will have to redo their work again since the original data may have changed. That seems like a lot of waste to me and (again, from a theoretical standpoint) not faster than a locked implementation or even a single-threaded implementation.
Also, just to give some context, I'm only using a dumb queue. There's nothing special or intensive about what I'm doing, it's just creating a node to add to the queue, and taking a node out. In the locked multi-threaded implementation, only the node creation part is not inside the lock(). Everything else is (essentially) single-threaded since it has to wait for the lock before adding or removing a node.
Is anyone able to explain why I'm getting the performance results that I'm getting, and generally, what advantages there are to lockless aside from what I'm already expecting (faster multi-threaded queue access).
|
|
|
|
|
Hi,
my best guess would be your test code is too simple: if all the test does is adding and removing queue entries, without any real calculations in between, then most CPU cycles are spent in the spin-loop inside Interlocked. IMO Interlocked is intended to and will work fine when contentions are rare, not when you create them all the time; under light contention conditions, I expect it to work more efficient than any other synchronization scheme.
To confirm this, you should try and come up with a test where extra CPU cycles are required to process the nodes; I would recommend adding something until you see the number of queue/dequeue operations go down by at least 80%, then for that computational load compare again the two schemes.
|
|
|
|
|
I put another variable in my lockless queue to measure the retry count (the number of times it has to spin again because something changed), and in my test bench, it seems that an additional 50% of operations are done, so 100 enqueues ends up being something like 100 successful operations and 50 respins, approximately. I'm not sure how well my numbers compare with other implementations, but that does seem like a lot. It seems from the result that the lockless implementation works best with little contention, but I'm curious to know the theory behind it.
|
|
|
|
|
Cyrilix wrote: measure the retry count (the number of times it has to spin again because something changed)
Not sure I understand what you mean. Care to share some code here?
Cyrilix wrote: I'm curious to know the theory behind it.
You could use reflector to investigate how it has been implemented in .NET although I expect you would also need to study the IL specification and still will be kept in the dark on how the native code looks (it is very likely they use special assembly instructions in the end).
|
|
|
|
|
My internet was down for a little so I couldn't post right away.
public void Enqueue(T data)
{
LockFreeQueueNode<T> tempTail = null;
LockFreeQueueNode<T> tempTailNext = null;
LockFreeQueueNode<T> newNode = new LockFreeQueueNode<T>(data);
newNode.Data = data;
do
{
tempTail = tail;
tempTailNext = tempTail.NextNode;
if (tempTail == tail)
{
if (tempTailNext == null)
{
if (Interlocked.CompareExchange(ref tempTail.NextNode, newNode, tempTailNext) == tempTailNext)
break;
}
else
{
Interlocked.CompareExchange(ref tail, tempTailNext, tempTail);
}
}
Interlocked.Increment(ref retryCount);
} while (true);
Interlocked.CompareExchange(ref tail, newNode, tempTail);
Interlocked.Increment(ref count);
}
I believe this code was actually taken somewhere from a Codeproject article (but I happened to find it online from a different source). The part that's in bold is the important line that I added to measure the retry count, basically everytime we are unable to break out of the while loop and need to retry the operation again.
|
|
|
|
|
Hi,
OK, if retryCount goes up it tells me nothing much is happening outside your queue; you and I agree on
"lockless implementation works best with little contention" and to reduce contention a lot of things should be going on outside the queue.
|
|
|
|
|
Contention is always bad, but generally lockless code should be faster under contention than using locks.
With lockless code, when multiple threads use CompareExchange concurrently, at least one will succeed. So you're not wasting any time compared to the locked version.
Did you include the Interlocked.Increment(ref retryCount) when timing?
Like locks, interlocked operations aren't cheap, so you should avoid doing too many of them. Do you really need "count"?
Currently you're doing at least 3 interlocked operations per insertion - maybe a lock really is faster in this case.
And what hardware did you test it on? How many cores? Are you using hyper-threading?
You might be better off by slowing down the retry operation - on hyper-threaded processors, retrying too often slows down the other thread running on that processor. And in general, all threads constantly accessing memory isn't a good idea. Try adding a small Thread.SpinWait when retrying.
|
|
|
|
|
Yes, I include everything when timing. I also saw that count could be eliminated to no negative effect on the algorithm itself. I just wouldn't have anything to keep track of approximately what the count is, which is OK.
The hardware that I'm running it on is an Athlon64 X2 4400+ (2.2 GHz) with 2 cores and no hyper-threading. One thing I did was instead of slowing down the retry, I slowed down the enqueue/dequeue rate, by adding some other intensive math in between. This reduces the contention rate significantly. Before, I'd have about 50% extra operations... slowing it down to a reasonable level drops the amount of additional operations down to 4-6%. Under such a case, the lockless algorithm is equal or better to the lock algorithm.
One question though, what do you mean to imply when you say "With lockless code, when multiple threads us CompareExchange concurrently, at least one will succeed." Isn't it the same with locks? When multiple threads try to enter a critical section, at least one, and only one will succeed. The others will wait their turn.
|
|
|
|
|
Its not so much the fact that its lockless but the implementation is slow.. Each enqueue and dequeue here require adding a new class ( which is the node) , a new is VERY expensive and uses a lock around the GC allocator. So you have removed the lock around enqueue and dequeue but hit the worse GC lock. A per formant lock less queue would pre-create and re-use its nodes OR manage its own memory with structs and unsafe. An array based queue would not require new nodes and only needed to new when growing the array and hence would offer much better base performance but is harder to make lock less.
secondly you are making it worse with a second interlock. Interlocks are not cheap they cause the pipeline to stall and lock they are cheaper than a full lock , note however most full locks just use an interlock on a variable and block if it is in use , since an enqueue without a new is FAST the lock will not really be active long and the other lock implementations ends up just being an interlock compare.
Now adding to a queue is probably a very cheap operation maybe 10 cycles so probably no contention with only 5 threads if they do normal work. ( In a bench mark it generate contention but this is not a real scenario a program would spend very little time doing the enqueue and dequeue.
Ben
|
|
|
|
|
I see what you mean. The second interlocked operation was removed (because it was more or less useless for a real data structure), but the implementation could be better. Thanks for the tip.
|
|
|
|
|
dear friends
how to convert a header file in c language into its c# equalent
this is my code.... help me out guys just give me ideas
void nrerror(char error_text[]);
int *ivector(int nl, int nh);
double *dvector(int nl,int nh);
char *cvector(int nl, int nh);
int **imatrix(int nrl,int nrh,int ncl,int nch);
double **dmatrix(int nrl,int nrh,int ncl,int nch);
void print_iMatrix(FILE *fp, int **imatrix, int rows, int cols);
void print_dMatrix(FILE *fp, double **dmatrix, int rows, int cols);
void free_dvector(double*v,int nl,int nh);
void free_cvector(char * c,int nl,int nh);
void free_ivector(int * v,int nl,int nh);
void free_imatrix(int **m ,int nrl,int nrh,int ncl,int nch);
void free_dmatrix(double ** m,int nrl,int nrh,int ncl,int nch);
|
|
|
|
|
lawrenceinba wrote: int *ivector(int nl, int nh);
if you don't want to use unsafe code, just use int . int ivector(int nl, int nh); will be the equivalent. But in C#, this function will return the value not the pointer.
lawrenceinba wrote: int **imatrix(int nrl,int nrh,int ncl,int nch);
Same like the above. You can use ref or out with the parameters and assign value to there. Let your method return void. It is same pattern like int.TryParse(string,out int)
In C#, all primitive types are passed by value. To make it pass by reference, you have to use ref or out with it. So all pointer variables (int*,double*) should be marked with ref or out to pass as reference.
|
|
|
|
|