IdentifiantMot de passe
Loading...
Mot de passe oubli� ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les r�ponses en temps r�el, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

multi threading c++


Sujet :

C++

  1. #1
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut multi threading c++
    Bonjour, J'ai fais un programme pour la lecture des graphes � partir d'un fichier:
    Code x : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    t # 0
    v 0 0
    v 1 3
    v 2 9
    e 2 1 68
    e 0 1 10
    e 0 2 4
    t # 1
    v 0 2
    v 1 11
    v 2 6
    v 3 10
    v 4 18
    v 5 14
    e 0 1 15
    e 2 5 19
    e 1 3 20
    t # 2
    v 0 6
    v 1 11
    e 0 1 13
    t # 3
    v 0 2
    v 1 11
    v 2 19
    v 3 2
    e 0 1 15
    e 1 2 11
    e 0 3 19
    t # 4
    v 0 1
    v 1 16
    v 2 14
    e 0 1 8
    e 1 2 5
    e 0 2 19

    et je veux l'am�liorer surtout en ce qui concerne le temps d�ex�cution, donc j'ai pens� � utiliser le multithreading mais j'ai aucune id�e d'o� commencer. J'ai lu quelques exemples (la plupart utilise les m�thodes et les fonctions qui ne sont pas dans mon programme).

    J'attends vos propositions.

    voila le code:
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/vf2_sub_graph_iso.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <ctime>
    using namespace std;
    using namespace boost;
     
    /*********************************************/
    // vertex
    struct VertexProperties {
        int id;
        int label;
        VertexProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // edge
    struct EdgeProperties {
        unsigned id;
        unsigned label;
        EdgeProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // Graph
    struct GraphProperties {
        unsigned id;
        unsigned label;
        GraphProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // adjency list
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
            VertexProperties,
            EdgeProperties,
            GraphProperties> Graph;
     
    // descriptors
     
    typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
    typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
    // iterators
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    typedef graph_traits<Graph>::edge_iterator edge_iter;
     
    int main()
    {
        clock_t start=std::clock();
        std::vector<Graph> dataG;
     
        std::ifstream file_reader("1000.data"); // flux d'entrée pour opérer sur les fichiers.
        //ifstream * file_reader= new ifstream("60.txt" ); //flux d'entrée pour opérer sur les fichiers.
     
        std::string line;
        while (std::getline(file_reader, line)) { // getline reads characters from an input stream and places them into a string
     
            char lineType;
     
            std::stringstream ss(line);  // use a string buffer that contains a sequence of characters.
            if (ss >> lineType) switch (lineType) {
                case 't':
                    {
                        char diez;
                        unsigned gid;
                        if (ss >> diez >> gid) {
                            dataG.emplace_back(GraphProperties (gid, gid));
                            //dataG.push_back(GraphProperties (gid, gid));
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'v':
                    {
                        assert(!dataG.empty());
     
                        int vId, vLabel;
                        if (ss >> vId >> vLabel) {
                            boost::add_vertex(VertexProperties(vId, vLabel), dataG.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'e':
                    {
                        assert(!dataG.empty());
     
                        int fromId, toId, vLabel;
                        if (ss >> fromId >> toId >> vLabel) {
                            // Note that the EdgeProperty.id doesn't make sense with your input data
                            // as it only contains [vertexFrom vertexTo edgeData]
                            boost::add_edge(fromId, toId, EdgeProperties(vLabel, vLabel), dataG.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
            } else
            {
                // ignoring empty line
            }
        }
    }

  2. #2
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    As-tu id�e de ce que tu veux distribuer?
    Qu'y gagneras-tu?

    Sans r�ponses � ces deux questions, n'essaie m�me pas de multi-threader.
    En g�n�ral, ajouter un thread permet de s�parer par exemple l'interface graphique du traitement.

    Ajouter des threads ne peut servir que si on sait qu'on y gagnera.
    Dans le cas contraire, ca risque de ralentir l'ex�cution du programme, et ca va forc�ment beaucoup ralentir son d�veloppement et sa clart�.

  3. #3
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    Au fait, je veux utiliser les threads pour la lecture de chaque graphe pour gagner plus de temps.
    j'ai modifi� le programme pr�c�dent:
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/vf2_sub_graph_iso.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <ctime>
    using namespace std;
    using namespace boost;
     
    /*********************************************/
    // vertex
    struct VertexProperties {
        int id;
        int label;
        VertexProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // edge
    struct EdgeProperties {
        unsigned id;
        unsigned label;
        EdgeProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // Graph
    struct GraphProperties {
        unsigned id;
        unsigned label;
        GraphProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // adjency list
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 
            VertexProperties, 
            EdgeProperties,
            GraphProperties> Graph;
     
    // descriptors
     
    typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
    typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
    // iterators
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    typedef graph_traits<Graph>::edge_iterator edge_iter;
    //==================================================================================
    std::vector<Graph> readandcreategraphs(string fich)
    {
    	std::vector<Graph> vect;
     
        std::ifstream file_reader(fich); // flux d'entrée pour opérer sur les fichiers.
        //ifstream * file_reader= new ifstream("60.txt" ); //flux d'entrée pour opérer sur les fichiers.
     
        std::string line;
        while (std::getline(file_reader, line)) { // getline reads characters from an input stream and places them into a string
     
            char lineType;
     
            std::stringstream ss(line);  // use a string buffer that contains a sequence of characters.
            if (ss >> lineType) switch (lineType) {
                case 't':
                    {
                        char diez;
                        unsigned gid;
                        if (ss >> diez >> gid) {
                            vect.emplace_back(GraphProperties (gid, gid));//1000.data>>3,381s
                            //vect.push_back(GraphProperties (gid, gid));//1000.data>>3,7776s
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'v':
                    {
                        assert(!vect.empty());
     
                        int vId, vLabel;
                        if (ss >> vId >> vLabel) {
                            boost::add_vertex(VertexProperties(vId, vLabel), vect.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'e':
                    {
                        assert(!vect.empty());
     
                        int fromId, toId, vLabel;
                        if (ss >> fromId >> toId >> vLabel) {
                            // Note that the EdgeProperty.id doesn't make sense with your input data
                            // as it only contains [vertexFrom vertexTo edgeData]
                            boost::add_edge(fromId, toId, EdgeProperties(vLabel, vLabel), vect.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
            } else
            {
                // ignoring empty line
            }
        }
     return vect;
    }
     
    //==================================================================================
     
    int main() 
    {
    	clock_t start=std::clock();
    	std::vector<Graph> dataG= readandcreategraphs("1000.data");
    //std::cout << "Lecture et creation de:" << dataG.size() << " graphs " << std::endl;
       	Graph testg;
       	typedef std::pair<edge_iter, edge_iter> edge_pair;
     
        cout<<"TIME: "<<( std::clock() - start ) / (double) CLOCKS_PER_SEC<<"s"<<endl; // fin du programme.
     
    }

  4. #4
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Tu n'y gagneras rien, parce que le probl�me c'est que tes graphes sont dans un/des fichiers, et qu'il faut lire ces fichiers.
    Ce n'est pas tellement le graphe qui pose probl�me, mais le disque dur.

    �ventuellement, recherche du cot� de mmap.

  5. #5
    Membre Expert
    Avatar de imperio
    Homme Profil pro
    �tudiant
    Inscrit en
    Mai 2010
    Messages
    872
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ain (Rh�ne Alpes)

    Informations professionnelles :
    Activit� : �tudiant
    Secteur : High Tech - �diteur de logiciels

    Informations forums :
    Inscription : Mai 2010
    Messages : 872
    Par d�faut
    Si il fait du traitement sur les fichiers, il peut threader l'appel � mmap pendant que le thread principal fait son traitement, �a sera toujours �a de gagner.

  6. #6
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Si tu d�butes avec C++, tu n'en es pas � threader.
    Un thread n'est pas un baguette magique, c'est un moyen de r�partir le travail.

    Encore faut-il savoir quel travail peut-�tre fait � cot� d'un autre, et en quoi ca n'utilise pas le m�me ressources (ici un fichier)

  7. #7
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    Citation Envoy� par leternel Voir le message
    Si tu d�butes avec C++, tu n'en es pas � threader.
    Un thread n'est pas un baguette magique, c'est un moyen de r�partir le travail.

    Encore faut-il savoir quel travail peut-�tre fait � cot� d'un autre, et en quoi ca n'utilise pas le m�me ressources (ici un fichier)

    Pour gagner en temps je veux cr�er un thread qui g�n�re un graphe et l'ajoute au vecteur pendant que le programme principal continue � lire le fichier. sinon existe-t-il un autre moyen pour am�lior� le programme?

  8. #8
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    Citation Envoy� par leternel Voir le message
    Tu n'y gagneras rien, parce que le probl�me c'est que tes graphes sont dans un/des fichiers, et qu'il faut lire ces fichiers.
    Ce n'est pas tellement le graphe qui pose probl�me, mais le disque dur.

    �ventuellement, recherche du cot� de mmap.
    �a veux dire quoi le mmap ?

  9. #9
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Tu ne peux pas continuer � lire le fichier pendant que tu le lis.
    Si encore tu avais plusieurs fichiers, tu pourrais charger chacun dans un thread (en r�utilisant les threads qui ont d�j� fini... cf thread pool)

    Un fichier, c'est rare et cher. c'est m�me pour ca qu'un fstream n'est pas copiable.
    Ca n'est pas vraiment partageable entre threads.

    En regardant ton code, tout me parait l�gitime.
    Par contre, je pense que tu ne compiles pas avec les optimisations de performances.
    Pour gcc, c'est -O2, voire -O3, si vraiment c'est n�cessaire.



    mmap est une fonction qui permet de "mapper en m�moire" un fichier, et donc de charger un fichier plus vite.
    Par contre, derri�re, tu le manipules comme un �norme tableau de char.

  10. #10
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    Citation Envoy� par leternel Voir le message
    Tu ne peux pas continuer � lire le fichier pendant que tu le lis.
    Si encore tu avais plusieurs fichiers, tu pourrais charger chacun dans un thread (en r�utilisant les threads qui ont d�j� fini... cf thread pool)

    Un fichier, c'est rare et cher. c'est m�me pour ca qu'un fstream n'est pas copiable.
    Ca n'est pas vraiment partageable entre threads.

    En regardant ton code, tout me parait l�gitime.
    Par contre, je pense que tu ne compiles pas avec les optimisations de performances.
    Pour gcc, c'est -O2, voire -O3, si vraiment c'est n�cessaire.



    mmap est une fonction qui permet de "mapper en m�moire" un fichier, et donc de charger un fichier plus vite.
    Par contre, derri�re, tu le manipules comme un �norme tableau de char.
    Au fait, apr�s la lecture d'un graphe (t #) je veux faire un thread qui s'occupe de la cr�ation du graphe , pendant que le thread fait les insertions n�cessaires, la lecture du fichier des graphes continue en parall�le.

  11. #11
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Et bien, c'est assez simple:
    Tu cr�es une structure premisse (un vector<std::string>?) contenant les lignes lues.
    Ta boucle de lecture consiste � "lire une premisse puis l'injecter dans une file, tant qu'il y a quelque chose � lire, puis clore la file."
    Ta boucle de cr�ation ressemble � "tant que la file n'est pas close, prendre une pr�misse et faire un graphe."

    Un thread, et puis c'est tout.
    Regarde du cot� de std::thread

  12. #12
    Membre Expert
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    1 415
    D�tails du profil
    Informations personnelles :
    Localisation : France, Paris (�le de France)

    Informations forums :
    Inscription : Mars 2007
    Messages : 1 415
    Par d�faut
    Citation Envoy� par leternel Voir le message
    Ta boucle de lecture consiste � "lire une premisse puis l'injecter dans une file, tant qu'il y a quelque chose � lire, puis clore la file."
    Ta boucle de cr�ation ressemble � "tant que la file n'est pas close, prendre une pr�misse et faire un graphe."
    Attention � la concurrence, la file doit �tre thread_safe. Il faudrait probablement utiliser des locks.

    Avant de vouloir multi-threader, il faut t'assurer que c'est vraiment cela qui pose probl�me. Et pour le faire, il faut profiler l'ex�cution de ton programme. Si apr�s profiling, IO disque utilise 90 % du temps d'ex�cution, �a sert � rien de t'ermmerder � multi-threader car tu n'as grand chose � gagner, tu pourrais m�me empirer la situation vu que tu es d�butant. Profling d'abord, optimisation apr�s.

  13. #13
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    Citation Envoy� par jblecanard Voir le message
    Attention � la concurrence, la file doit �tre thread_safe. Il faudrait probablement utiliser des locks.

    Avant de vouloir multi-threader, il faut t'assurer que c'est vraiment cela qui pose probl�me. Et pour le faire, il faut profiler l'ex�cution de ton programme. Si apr�s profiling, IO disque utilise 90 % du temps d'ex�cution, �a sert � rien de t'ermmerder � multi-threader car tu n'as grand chose � gagner, tu pourrais m�me empirer la situation vu que tu es d�butant. Profling d'abord, optimisation apr�s.
    Comment faire le Profling svp !

  14. #14
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Avec un profiler, tel que gprofile ou valgrind (si je ne me trompe pas).

    Cherche "<ton os> profiler c++".

  15. #15
    Membre Expert
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    1 415
    D�tails du profil
    Informations personnelles :
    Localisation : France, Paris (�le de France)

    Informations forums :
    Inscription : Mars 2007
    Messages : 1 415
    Par d�faut
    Les Google Perf Tools sont bien pour �a car pas trop co�teux � l'ex�cution, ce qui permet de mieux repr�senter le co�t de l'IO en elle-m�me.

  16. #16
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    J'ai utilis� gprof sur ce code
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
     
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/vf2_sub_graph_iso.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <ctime>
    using namespace std;
    using namespace boost;
     
    /*********************************************/
    // vertex
    struct VertexProperties {
        int id;
        int label;
        VertexProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // edge
    struct EdgeProperties {
        unsigned id;
        unsigned label;
        EdgeProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // Graph
    struct GraphProperties {
        unsigned id;
        unsigned label;
        GraphProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // adjency list
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, 
            VertexProperties, 
            EdgeProperties,
            GraphProperties> Graph;
     
    // descriptors
     
    typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
    typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
    // iterators
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    typedef graph_traits<Graph>::edge_iterator edge_iter;
    //==================================================================================
    std::vector<Graph> readandcreategraphs(string fich)
    {
    	std::vector<Graph> vect;
     
        std::ifstream file_reader(fich); // flux d'entrée pour opérer sur les fichiers.
        //ifstream * file_reader= new ifstream("60.txt" ); //flux d'entrée pour opérer sur les fichiers.
     
        std::string line;
        while (std::getline(file_reader, line)) { // getline reads characters from an input stream and places them into a string
     
            char lineType;
     
            std::stringstream ss(line);  // use a string buffer that contains a sequence of characters.
            if (ss >> lineType) switch (lineType) {
                case 't':
                    {
                        char diez;
                        unsigned gid;
                        if (ss >> diez >> gid) {
                            vect.emplace_back(GraphProperties (gid, gid));//1000.data>>3,381s
                            //vect.push_back(GraphProperties (gid, gid));//1000.data>>3,7776s
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'v':
                    {
                        assert(!vect.empty());
     
                        int vId, vLabel;
                        if (ss >> vId >> vLabel) {
                            boost::add_vertex(VertexProperties(vId, vLabel), vect.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'e':
                    {
                        assert(!vect.empty());
     
                        int fromId, toId, vLabel;
                        if (ss >> fromId >> toId >> vLabel) {
                            // Note that the EdgeProperty.id doesn't make sense with your input data
                            // as it only contains [vertexFrom vertexTo edgeData]
                            boost::add_edge(fromId, toId, EdgeProperties(vLabel, vLabel), vect.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
            } else
            {
                // ignoring empty line
            }
        }
     return vect;
    }
     
    //==================================================================================
     
    int main() 
    {
    	clock_t start=std::clock();
    	std::vector<Graph> dataG= readandcreategraphs("5.txt");
    //std::cout << "Lecture et creation de:" << dataG.size() << " graphs " << std::endl;
       	Graph testg;
       	typedef std::pair<edge_iter, edge_iter> edge_pair;
     
        cout<<"TIME: "<<( std::clock() - start ) / (double) CLOCKS_PER_SEC<<"s"<<endl; // fin du programme.
     
          typedef std::pair<edge_iter, edge_iter> edge_pair;
     
          int c = 0; //compteur de dataG
          for (auto gr : dataG)
          {
     
            std::cout << "* * * Graph " << c << " * * * contains " << num_vertices(gr) << " vertices, and " << num_edges(gr) << " edges " << std::endl;        
            //Vertex list
            std::cout << "  Vertex list: " << std::endl;
            for (size_t i = 0 ; i < num_vertices(gr) ; ++i) //size_t vertice number in the graph
            {
              std::cout << "   v[" << i << "]   ID: " << gr[i].id << ", Label: " << gr[i].label << std::endl;
            }
            //Edge list
            std::cout << "  Edge list: " << std::endl;
            edge_pair ep;
            for (ep = edges(gr); ep.first != ep.second; ++ep.first) //ep edge number 
            {
              vertex_t from = source(*ep.first, gr);
              vertex_t to = target(*ep.first, gr);
              edge_t edg = edge(from, to, gr);
              std::cout << "   e(" << gr[from].label << "," << gr[to].label << ")   ID: " << gr[edg.first].id << " ,  Label: " <<  gr[edg.first].label << std::endl;
            }
            std::cout << std::endl;
            c++;
          }
     
    }
    Voila ce que j'obtiens (aidez-moi � le comprendre svp)
    Fichiers attach�s Fichiers attach�s

  17. #17
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Et qu'en d�duis-tu?
    Perso, je ne vois que des 0, donc la partie profil�e (les fonctions non syst�me) sont d�j� sans impact sur les performances.

    C'est ton fichier qui ralenti tout.
    O� est-il? (disque dur local ou externe, lecteur r�seau)
    Quelle taille fait-il?

    Combien de temps prends ton programme sur ce fichier?

  18. #18
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    Le fichier se trouve dans le m�me emplacement du programme.

  19. #19
    Expert �minent

    Femme Profil pro
    Ing�nieur d�veloppement logiciels
    Inscrit en
    Juin 2007
    Messages
    5 202
    D�tails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : France, Essonne (�le de France)

    Informations professionnelles :
    Activit� : Ing�nieur d�veloppement logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 5 202
    Par d�faut
    Ca ne dit pas s'il est sur le r�seau ou non.
    Ton programme est peut-�tre lent � charger en m�moire, lui aussi.

    En gros, les performances sont limit�es par le plus lent sous-syst�me utilis�.
    Ici, il y en a que trois:
    l'acc�s au fichier
    le swap sur disque si les donn�es sont plus grandes que la ram r�ellement disponible.
    le processeur.

    Comme tu ne calcules pratiquement rien, le proc est hors de cause, reste l'acc�s au donn�e.
    D'ou ma question sur l'emplacement physique du fichier.

    En effet, la bande passante d'un disque dur, c'est de l'ordre du Go/s, la ram est au moins dix fois plus rapide, tandis que les r�seaux les plus performants atteigne le Gbits/s, soit 8 fois moins qu'un disque.
    et l'usb est plus lent qu'un disque, mais moins que le r�seau (je n'ai plus les chiffres).

  20. #20
    Membre tr�s actif
    Homme Profil pro
    �tudiant
    Inscrit en
    Avril 2011
    Messages
    338
    D�tails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Alg�rie

    Informations professionnelles :
    Activit� : �tudiant

    Informations forums :
    Inscription : Avril 2011
    Messages : 338
    Par d�faut
    J'ai modifi� le programme, maintenant il lit tout le fichier pour le mettre dans une variable de type string, puis il va faire le traitement n�cessaire et la cr�ation des graphes.Comment utiliser les threads maintenant !
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/graph/vf2_sub_graph_iso.hpp>
    #include <boost/algorithm/string/split.hpp>
    #include <boost/algorithm/string/classification.hpp>
    #include <fstream>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <ctime>
    using namespace std;
    using namespace boost;
     
    /*********************************************/
    // vertex
    struct VertexProperties {
        int id;
        int label;
        VertexProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // edge
    struct EdgeProperties {
        unsigned id;
        unsigned label;
        EdgeProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // Graph
    struct GraphProperties {
        unsigned id;
        unsigned label;
        GraphProperties(unsigned i=0, unsigned l=0) : id(i), label(l) {}
    };
     
    // adjency list
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
            VertexProperties,
            EdgeProperties,
            GraphProperties> Graph;
     
    // descriptors
     
    typedef boost::graph_traits<Graph>::vertex_descriptor vertex_t;
    typedef std::pair<boost::graph_traits<Graph>::edge_descriptor, bool> edge_t;
    // iterators
    typedef graph_traits<Graph>::vertex_iterator vertex_iter;
    typedef graph_traits<Graph>::edge_iterator edge_iter;
    //==================================================================================
    std::string readfromfile(string fich)
    {
        ifstream inFile;
        inFile.open(fich);//open the input file
     
        stringstream strStream;
        strStream << inFile.rdbuf();//read the file
        return strStream.str();//str holds the content of the file
     
    }
    //===========================================================
    vector<string>  splitstringtolines(string str)
    {
        vector<string> split_vector;
        split(split_vector, str, is_any_of("\n"));
     
        return split_vector;
    }
    //============================================================
    std::vector<string> readgraphs(string fich)
    {
     
     
        std::ifstream file_reader(fich); // flux d'entrée pour opérer sur les fichiers.
        //ifstream * file_reader= new ifstream("60.txt" ); //flux d'entrée pour opérer sur les fichiers.
     
        std::string l;
        std::vector<string> fichlines;
        while (std::getline(file_reader, l))
        { // getline reads characters from an input stream and places them into a string
            fichlines.emplace_back(l);       
        }
     
     
     return fichlines;
    }
     
    //==============================================================
    std::vector<Graph> creategraphs(std::vector<string> fichlines)
    {
        std::vector<Graph> vect;
        for (string line:fichlines)
        {
            char lineType;
     
            std::stringstream ss(line);  // use a string buffer that contains a sequence of characters.
            if (ss >> lineType) switch (lineType) {
                case 't':
                    {
                        char diez;
                        unsigned gid;
                        if (ss >> diez >> gid) {
                            vect.emplace_back(GraphProperties (gid, gid));//1000.data>>3,381s
                            //vect.push_back(GraphProperties (gid, gid));//1000.data>>3,7776s
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'v':
                    {
                        assert(!vect.empty());
     
                        int vId, vLabel;
                        if (ss >> vId >> vLabel) {
                            boost::add_vertex(VertexProperties(vId, vLabel), vect.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
                case 'e':
                    {
                        assert(!vect.empty());
     
                        int fromId, toId, vLabel;
                        if (ss >> fromId >> toId >> vLabel) {
                            // Note that the EdgeProperty.id doesn't make sense with your input data
                            // as it only contains [vertexFrom vertexTo edgeData]
                            boost::add_edge(fromId, toId, EdgeProperties(vLabel, vLabel), vect.back());
                        }
                        else throw std::runtime_error("Error parsing '" + line + "'");
                    }
                    break;
            } else
            {
                // ignoring empty line
            }
     
        }
        /*
         
        */
     
        return vect;
    }
     
     
    int main()
    {
        clock_t start=std::clock();
        std::vector<Graph> data =creategraphs(splitstringtolines(readfromfile("60.txt")));
        cout<<">>>>>>>> TIME: "<<( std::clock() - start ) / (double) CLOCKS_PER_SEC<<"s"<<endl;
    }

    et comment acc�der au information du graphe?
    Code : S�lectionner tout - Visualiser dans une fen�tre � part
    std::cout << "* * * Graph " << gr.label << std::endl;
    ne fonctionne pas

Discussions similaires

  1. Tri multi-thread�
    Par Tifauv' dans le forum C
    R�ponses: 8
    Dernier message: 28/06/2007, 09h00
  2. R�ponses: 2
    Dernier message: 15/05/2004, 18h33
  3. R�ponses: 16
    Dernier message: 30/01/2004, 11h05
  4. [VB6][active x] faire du multi-thread avec vb
    Par pecheur dans le forum VB 6 et ant�rieur
    R�ponses: 9
    Dernier message: 20/05/2003, 12h01
  5. [Kylix] exception qtinft.dll et multi-threading
    Par leclaudio25 dans le forum EDI
    R�ponses: 3
    Dernier message: 27/03/2003, 18h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo